JavaScript
x
2
1
lis = [ [12,34,56],[45,78,334],[56,90,78],[12,34,56] ]
2
I want the result to be 2 since number of duplicate lists are 2 in total. How do I do that?
I have done something like this
JavaScript
1
6
1
count=0
2
for i in range(0, len(lis)-1):
3
for j in range(i+1, len(lis)):
4
if lis[i] == lis[j]:
5
count+=1
6
But the count value is 1 as it returns matched lists. How do I get the total number of duplicate lists?
Advertisement
Answer
Solution
You can use collections.Counter
if your sub-lists only contain numbers and therefore are hashable:
JavaScript
1
8
1
>>> from collections import Counter
2
>>> lis = [[12, 34, 56], [45, 78, 334], [56, 90, 78], [12, 34, 56]]
3
>>> sum(y for y in Counter(tuple(x) for x in lis).values() if y > 1)
4
2
5
>>> lis = [[12, 34, 56], [45, 78, 334], [56, 90, 78], [12, 34, 56], [56, 90, 78], [12, 34, 56]]
6
>>> sum(y for y in Counter(tuple(x) for x in lis).values() if y > 1)
7
5
8
In Steps
Convert your sub-list into tuples:
JavaScript
1
2
1
tuple(x) for x in lis
2
Count them:
JavaScript
1
3
1
>>> Counter(tuple(x) for x in lis)
2
Counter({(12, 34, 56): 3, (45, 78, 334): 1, (56, 90, 78): 2})
3
take only the values:
JavaScript
1
3
1
>>> Counter(tuple(x) for x in lis).values()
2
dict_values([3, 1, 2])
3
Finally, sum only the ones that have a count greater than 1:
JavaScript
1
3
1
> sum(y for y in Counter(tuple(x) for x in lis).values() if y > 1)
2
5
3
Make it Re-usable
Put it into a function, add a docstring, and a doc test:
JavaScript
1
35
35
1
"""Count duplicates of sub-lists.
2
"""
3
4
5
from collections import Counter
6
7
8
def count_duplicates(lis):
9
"""Count duplicates of sub-lists.
10
11
Assumption: Sub-list contain only hashable elements.
12
Result: If a sub-list appreas twice the result is 2.
13
If a sub-list aprears three time and a other twice the result is 5.
14
15
>>> count_duplicates([[12, 34, 56], [45, 78, 334], [56, 90, 78],
16
12, 34, 56]]) [
17
2
18
>>> count_duplicates([[12, 34, 56], [45, 78, 334], [56, 90, 78],
19
12, 34, 56], [56, 90, 78], [12, 34, 56]]) [
20
21
5
22
"""
23
# Make it a bit more verbose than necessary for readability and
24
# educational purposes.
25
tuples = (tuple(elem) for elem in lis)
26
counts = Counter(tuples).values()
27
return sum(elem for elem in counts if elem > 1)
28
29
30
if __name__ == '__main__':
31
32
import doctest
33
34
doctest.testmod(verbose=True)
35
Run the test:
JavaScript
1
21
21
1
python count_dupes.py
2
Trying:
3
count_duplicates([[12, 34, 56], [45, 78, 334], [56, 90, 78],
4
[12, 34, 56]])
5
Expecting:
6
2
7
ok
8
Trying:
9
count_duplicates([[12, 34, 56], [45, 78, 334], [56, 90, 78],
10
[12, 34, 56], [56, 90, 78], [12, 34, 56]])
11
Expecting:
12
5
13
ok
14
1 items had no tests:
15
__main__
16
1 items passed all tests:
17
2 tests in __main__.count_duplicates
18
2 tests in 2 items.
19
2 passed and 0 failed.
20
Test passed.
21