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.
myArray=[1,1,1,1,1,1,1,1,2]
I would like it to return: True
if any value occurs more than twice .
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
import collections
arr= [1,2,3,5,6]
Counter(arr)
Returns: Counter({1: 1, 2: 1, 3: 1, 5: 1, 6: 1})
You can use collections.Counter for this:
from collections import Counter
print any(count > 2 for count in Counter(myArray).itervalues()) # True
Or, if you are using 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. 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.
Here's one way using collections.defaultdict
. Like @HoriaComan's method, this solution doesn't require iterating your entire list.
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:
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()
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])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.