【发布时间】: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`
【问题讨论】:
-
开放式的“我如何改进这个工作代码”问题更适合Code Review。请记住先阅读他们的指南:codereview.stackexchange.com/help/how-to-ask
-
这回答了你的问题了吗? Common values in a Python dictionary
标签: python dictionary feedback