python - 检查字典值中是否存在值
问题描述
我有这本词典
score_dict = \
{5: ['Far above average', 'Extremely well', 'Extremely effective', 'Extremely fast', 'Always', 'Strongly agree', 'Far exceeds expectations'],
4: ['Somewhat above average', 'Very well', 'Very effective', 'Somewhat fast', 'Most of the time', 'Somewhat agree', 'Exceeds expectations'],
3: ['Average', 'Moderately well', 'Moderately effective', 'Average', 'About half the time', 'Neither agree nor disagree', 'Equals expectations'],
2: ['Somewhat Below Average', 'Slightly well', 'Slightly effective', 'Somewhat slow', 'Sometimes', 'Somewhat disagree', 'Short of expectations'],
1: ['Far Below Average', 'Not well at all', 'Not effective at all', 'Extremely slow', 'Never', 'Strongly disagree', 'Far short of expectations'],
}
为什么这不打印True
?
if 'Far above average' in score_dict.values():
print("True")
解决方案
这不会打印出来,因为其中的项目.values()
是列表。
一种方法如下:
for l in score_dict.values():
if 'Far above average' in l:
print("True")
推荐阅读
- python - Python:两个系列之间的互相关
- java - org.apache.maven.lifecycle.LifecycleExecutionException: 未能执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.5.1
- javascript - URL.createObjectUrl() 如何成为模块?
- javascript - 如何在 JavaScript 函数中创建对按钮的引用以避免重复?
- javascript - Create a custom/prettier confirm() and use it in if() statement
- wildfly - 如果知道原始密码,是否可以更改 Wildfly 凭证存储的密码?
- javascript - 类成员方法未绑定到 TypeScript -> JavaScript
- mysql - 如何让数字位于小数点前2位?
- python - Matplotlib:如何使用用户输入更改颜色条渐变
- sharepoint - 在 Microsoft Teams webhook 消息中附加 Sharepoint 文件