[英]Is there any function to find the mode (or multiple if more than 1 value occurs most) of a list in Python 2.6?
[英]How to determine if any value occurs more than twice in a list?
我有一个列表,并想确定是否有任何值出现两次以上。 我已经尝试过使用集合和计数器,但我无法将它们评估为单个True或False值。
myArray=[1,1,1,1,1,1,1,1,2]
我希望它返回:如果任何值出现超过两次,则为True
。
任何帮助都表示赞赏,如果解决方案很快就会有所帮助。 我正在检查数十万个列表。 我是编程新手,这是我的第一篇文章。
编辑:我的尝试,我也是stackoverflow UI的新手
import collections
arr= [1,2,3,5,6]
Counter(arr)
返回: Counter({1: 1, 2: 1, 3: 1, 5: 1, 6: 1})
您可以使用collections.Counter :
from collections import Counter
print any(count > 2 for count in Counter(myArray).itervalues()) # True
或者,如果您使用的是Python 3:
from collections import Counter
print(any(count > 2 for count in Counter(myArray).values())) # True
您始终可以构造值的直方图,并查看是否有任何条目大于2。 它可能看起来像这样:
def is_more_than_twice(l):
hist = {}
for el in l:
if el in hist:
hist[el] += 1
else:
hist[el] = 1
if hist[el] > 2:
return True
return False
您不需要迭代直到列表的末尾,直到您遇到看到元素el
出现两次以上的情况。
这是使用collections.defaultdict
的一种方法。 与@ HoriaComan的方法一样,此解决方案不需要迭代整个列表。
myArray = [1,1,1,1,1,1,1,1,2]
from collections import defaultdict
def count_limit(L, k=2):
d = defaultdict(int)
for item in L:
if d[item] == k:
return True
else:
d[item] += 1
return False
res = count_limit(myArray) # False
绩效基准
为了证明这种影响,我们可以在更大的迭代Counter
上与Counter
进行比较:
myArray = [1,1,1,1,1,1,1,1,2]*10000
from collections import defaultdict, Counter
def count_limit(L, k=2):
d = defaultdict(int)
for item in L:
if d[item] == k:
return True
else:
d[item] += 1
return False
def count_limit_counter(myArray):
return any(count > 2 for count in Counter(myArray).values())
%timeit count_limit(myArray) # 1.52 µs per loop
%timeit count_limit_counter(myArray) # 6.64 ms per loop
尝试使用set()
def OccursMoreThanTwice(myArray):
for e in myArray:
if myArray.count(e) > 2:
return True
return False
print OccursMoreThanTwice([1,1,1,1,1,1,1,1,2])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.