Tai*_*mur 5 python arrays list
我基本上有一个50个整数的数组,我需要找出50个整数中的任何一个是否相等,如果它们是,我需要执行一个动作.
我该怎么做呢?据我所知,目前Python中没有一个函数可以实现吗?
如果您的意思是您有一个列表并且想知道是否有任何重复值,那么从列表中创建一个集合,看它是否比列表短:
if len(set(my_list)) < len(my_list):
print "There's a dupe!"
Run Code Online (Sandbox Code Playgroud)
但是,这不会告诉您重复值是什么.
如果你有Python 2.7+,你可以使用Counter.
>>> import collections
>>> input = [1, 1, 3, 6, 4, 8, 8, 5, 6]
>>> c = collections.Counter(input)
>>> c
Counter({1: 2, 6: 2, 8: 2, 3: 1, 4: 1, 5: 1})
>>> duplicates = [i for i in c if c[i] > 1]
>>> duplicates
[1, 6, 8]
Run Code Online (Sandbox Code Playgroud)