Reputation: 1960
If I have a list of dictionary or a list of list where each elements are equal sized, e.g. 2 elements → [{1,2}, {3,4}, {4,6}, {1,2}]
or [[1,2], [3,4], [4,6], [1,2]]
How do I check for duplicate and keep count of repeated times?
For list, something like this would work but I can't use set directly in my case.
recur1 = [[x, status.count(x)] for x in set(list1)]
Upvotes: 0
Views: 65
Reputation: 2047
ll = [[1,2], [3,4], [4,6], [1,2]]
# Step1 Using a dictionary.
counterDict = {}
for l in ll:
key = tuple(l) # list can not be used as a dictionary key.
if key not in counterDict:
counterDict[key] = 0
counterDict[key] += 1
print(counterDict)
# Step2 collections.Counter()
import collections
c = collections.Counter([ tuple(l) for l in ll])
print(c)
# Step3 list.count()
for l in ll:
print(l , ll.count(l))
Upvotes: 0
Reputation: 71451
You can use Counter from collections:
from collections import Counter
the_list = [[1,2], [3,4], [4,6], [1,2]]
new_list = map(tuple, the_list)
the_dict = Counter(new_list)
final_list = [a for a, b in the_dict.items() if b > 1]
#the number of duplicates:
print len(final_list)
#the duplicates themselves:
print final_list
if len(final_list) > 0:
print "Duplicates exist in the list"
print "They are: "
for i in final_list:
print i
else:
print "No duplicates"
Upvotes: 0
Reputation: 95948
The easiest way is to use a Counter
, but you have to convert to a hashable (i.e. immutable) type:
>>> from collections import Counter
>>> objs = [{1,2}, {3,4}, {4,6}, {1,2}]
>>> counts = Counter(objs)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/juan/anaconda3/lib/python3.5/collections/__init__.py", line 530, in __init__
self.update(*args, **kwds)
File "/Users/juan/anaconda3/lib/python3.5/collections/__init__.py", line 617, in update
_count_elements(self, iterable)
TypeError: unhashable type: 'set'
So, for a set, the natural choice is a frozenset
:
>>> counts = Counter(frozenset(s) for s in objs)
>>> counts
Counter({frozenset({1, 2}): 2, frozenset({4, 6}): 1, frozenset({3, 4}): 1})
>>>
This is assuming order doesn't matter, although, you can create an OrderedCounter almost trivially...
If instead you have a list of lists, a tuple
would be the natural choice:
>>> objs = [[1,2], [3,4], [4,6], [1,2]]
>>> counts = Counter(tuple(l) for l in objs)
>>> counts
Counter({(1, 2): 2, (3, 4): 1, (4, 6): 1})
Upvotes: 2