以下代码显示列表中最不常见的重复数字并正常工作并给出答案3 ...
theList = [1,3,1,2,2]
listCheck, least = len(theList), 0
for theLeast in theList:
if theList.count(theLeast) <= listCheck :
listCheck = theList.count(theLeast)
least = theLeast
print (least)
然而,当我定义&amp;将列表更改为整数序列,代码表示1是最不常见的?
def The_uncommon(integers):
theList = integers
listCheck, least = len(theList), 0
for theLeast in theList:
if theList.count(theLeast) <= listCheck :
listCheck = theList.count(theLeast)
least = theLeast
return (least)
print(The_uncommon([1,3,1,2,2]))
欢迎任何建议
答案 0 :(得分:1)
小心缩进。 :)
在第一个片段中,您将遍历整个循环,而在定义函数的第二个示例中,您将返回第一个 least
次。
答案 1 :(得分:0)
python的经典缩进问题。
def The_uncommon(integers):
theList = integers
listCheck, least = len(theList), 0
for theLeast in theList:
if theList.count(theLeast) <= listCheck :
listCheck = theList.count(theLeast)
least = theLeast
return (least)
print(The_uncommon([1,3,1,2,2]))