【问题标题】:How do I find the common values in a python dictionary? [closed]如何在 python 字典中找到公共值? [关闭]
【发布时间】:2023-01-26 17:26:23
【问题描述】:

编写一个 Python 程序来打印字典中所有常见的值

我正在学习 python 并完成了这个任务。话虽如此,我知道我的代码很可能比需要的要长得多。我希望获得有关更简单的方法来获得相同解决方案的反馈,以及有关如何使我目前拥有的代码更高效和更易于阅读的提示。感谢您的帮助!:)

classNumbers = {'Physics' : 17,
      'Psychology' : 20,
      'Cryptography' : 14,
      'Chemistry' : 17,
      'Speech' : 23,
      'Art' : 13, 
      'Algebra' : 14,  
      'Law' : 20,                 
      'Anthropology' : 17,               
      'Photography' : 15,            
      'Calculus' : 25,            
      'Business' : 15}

valList = []
for value in classNumbers.values():
    valList.append(value)

i = 0
nvp = i + 1
mv = 0
nv = 0
vCount = 1
cvList = []

print('Here are all values in the classNumers dictionary:')
print('--------------------------------------------------')

while i != len(valList):
    mv = valList[i]
    while nvp < len(valList):
        nv = (valList[nvp])
        if nv == mv:
            vCount = vCount + 1 
        nvp = nvp + 1
    if mv in cvList:
        vCount = 0
    if  vCount != 0:
        print('Values: {} Count: {}'.format(mv, vCount))
    if vCount > 1:
        cvList.append(mv)
    vCount = 1
    i = i + 1
    nvp = i + 1
    
print('')
print('Out of all of the dictionary values, these are the common values')
print('----------------------------------------------------------------')

i = 0
while i != len(cvList):
    print(cvList[i])
    i = i + 1`
   

结果enter image description here

【问题讨论】:

标签: python dictionary feedback


【解决方案1】:

中有一个非常有用的Counter类收藏品这将是理想的模块。

from collections import Counter

classNumbers = {'Physics' : 17,
      'Psychology' : 20,
      'Cryptography' : 14,
      'Chemistry' : 17,
      'Speech' : 23,
      'Art' : 13, 
      'Algebra' : 14,  
      'Law' : 20,                 
      'Anthropology' : 17,               
      'Photography' : 15,            
      'Calculus' : 25,            
      'Business' : 15}

counter = Counter(classNumbers.values())

print('Here are all values in the classNumbers dictionary')
print('--------------------------------------------------')

for k, v in counter.items():
    print(f'Values: {k} Count: {v}')

print('
Out of all of the dictionary values, these are the common values')
print('----------------------------------------------------------------')

for n, v in counter.most_common():
    if v == 1:
        break
    print(n)

输出:

Here are all values in the classNumbers dictionary
--------------------------------------------------
Values: 17 Count: 3
Values: 20 Count: 2
Values: 14 Count: 2
Values: 23 Count: 1
Values: 13 Count: 1
Values: 15 Count: 2
Values: 25 Count: 1

Out of all of the dictionary values, these are the common values
----------------------------------------------------------------
17
20
14
15

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-08
    • 2011-07-16
    • 2012-04-12
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多