首页 > 解决方案 > 检查字典值中是否存在值

问题描述

我有这本词典

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")

标签: python

解决方案


这不会打印出来,因为其中的项目.values()是列表。

一种方法如下:

for l in score_dict.values():
    if 'Far above average' in l:
        print("True")

推荐阅读