[英]How to determine if any value occurs more than twice in a list?
I have a list and would like to determine if any value occurs more than twice. 我有一个列表,并想确定是否有任何值出现两次以上。 I've tried using collections and counter but I cannot get those to evaluate to a single True or False value. 我已经尝试过使用集合和计数器,但我无法将它们评估为单个True或False值。
myArray=[1,1,1,1,1,1,1,1,2]
I would like it to return: True
if any value occurs more than twice . 我希望它返回:如果任何值出现超过两次,则为True
。
Any help is appreciated and it would really help if the solution was fast. 任何帮助都表示赞赏,如果解决方案很快就会有所帮助。 I'm checking hundreds of thousands of lists. 我正在检查数十万个列表。 I'm new to programming and this is my first post. 我是编程新手,这是我的第一篇文章。
EDIT: my attempts, also I'm new to stackoverflow UI 编辑:我的尝试,我也是stackoverflow UI的新手
import collections
arr= [1,2,3,5,6]
Counter(arr)
Returns: Counter({1: 1, 2: 1, 3: 1, 5: 1, 6: 1})
返回: Counter({1: 1, 2: 1, 3: 1, 5: 1, 6: 1})
You can use collections.Counter for this: 您可以使用collections.Counter :
from collections import Counter
print any(count > 2 for count in Counter(myArray).itervalues()) # True
Or, if you are using Python 3: 或者,如果您使用的是Python 3:
from collections import Counter
print(any(count > 2 for count in Counter(myArray).values())) # True
You could always construct a histogram of the values and see if any entry is greater than two. 您始终可以构造值的直方图,并查看是否有任何条目大于2。 It could look something like this: 它可能看起来像这样:
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
You don't need to iterate until the end of the list, just until you've met the condition of seeing an element el
appearing more than twice. 您不需要迭代直到列表的末尾,直到您遇到看到元素el
出现两次以上的情况。
Here's one way using collections.defaultdict
. 这是使用collections.defaultdict
的一种方法。 Like @HoriaComan's method, this solution doesn't require iterating your entire list. 与@ 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
Performance benchmarking 绩效基准
To demonstrate the impact, we can compare with Counter
on a larger iterable: 为了证明这种影响,我们可以在更大的迭代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
Try it using set() 尝试使用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.