您会得到一组数字。打印出现最少的元素。如果元素多于1个,则按其值的降序打印所有元素。
输入:
[9, 1, 6, 4, 56, 56, 56, 6, 4, 2]
输出:
[9, 2, 1]
我实际上得到了输出,但没有执行私人案件,请帮助我。
from collections import Counter
n=int(input())
ans=""
list1=[]
list2=[]
list1=[int(x) for x in input().strip().split()][:n]
dict1=dict(Counter(list1))
k=min(dict1,key=dict1.get)
l=dict1[k]
for i,j in dict1.items():
if(j==l):
list2.append(i)
list2.reverse()
for i in list2:
ans+=str(i)+' '
print(ans[:-1])
我看到了很多复杂的答案。实际上,可以通过仅对以下实例中的项目使用列表理解来完成Counter()
:
>>> from collections import Counter
>>> count = Counter([9, 1, 6, 4, 56, 56, 56, 6, 4, 2])
>>> values = [key for key, value in count.items() if value == min(count.values())]
>>> values.sort(reverse=True) # [9, 2, 1]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句