我不明白为什么函数返回 4 而它应该返回 3。非常感谢。
x = [10,20,20,10,10,30,50,10,20]
s = {}
count = 0
for item in x:
if (item in s):
s[item] += 1
else:
s[item] = 1
for z, w in s.items():
count += w/2
print(int(count))
根据你对你所说的话的描述,想要计算对,那么我相信你会想要将被添加到计数的数字四舍五入而不是整体计数,因为两半最终会变成 1。
以下确实返回 3。
x = [10,20,20,10,10,30,50,10,20]
s = {}
count = 0
for item in x:
if (item in s):
s[item] += 1
else:
s[item] = 1
for z, w in s.items():
count += int(w/2)
print(count)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句