Skip to content Skip to sidebar Skip to footer

How To Count Number Of Duplicates In A List Of Tuples?

I have a python list of tuples as shown below: listoftups = [('A', 'B'), ('C','D'), ('E','F'), ('G','H'), ('A','B'), ('C','D')] I want to count the number of duplicates in this l

Solution 1:

You can use Counter

listoftups = [('A', 'B'), ('C','D'), ('E','F'), ('G','H'), ('A','B'), ('C','D')] 
from collections import Counter 
for k, v in Counter(listoftups).most_common():
    print "{} -> {} {}".format(k[0], k[1], v)

Output

A -> B 2
C -> D 2
G -> H 1
E -> F 1

Solution 2:

You can use the list's count method:

listoftups = [('A', 'B'), ('C','D'), ('E','F'), ('G','H'), ('A','B'), ('C','D')] 
tup = listoftups.count(('A', 'B')) # returns 2

and to count them all into a dictionary:

result = dict()
for tup in set(listoftups):
    result[tup] = listoftups.count(tup)

or more succinctly with a dictionary comprehension:

result = {tup:listoftups.count(tup) for tup in set(listoftups)}

After you would have a dictionary of:

result = { ('A', 'B'): 2, ('C', 'D'): 2, ('E','F'): 1, ('G', 'H'): 1}

and you can print it the same way that fourtheye does it or:

for k, v in result.items():
    print k[0] + "->" + k[1] + " ", v

Solution 3:

First of all, get a list with non-duplicated items using a set. Then iterate through them, and print them in the format desired, using count:

listoftups = [('A', 'B'), ('C','D'), ('E','F'), ('G','H'), ('A','B'), ('C','D')] 

listoftups = list(set(listoftups))
for el in listoftups:
    print "{} -> {} {}".format(el[0], el[1], listoftups.count(el))

If you want to preserve the order, create the unique values like this:

tmp = []
for el in listoftups:
    if el not in tmp:
        tmp.append(el)

And then do the for loop i did in the first example.


Solution 4:

import collections
result = collections.defaultdict(int)
def f(tup):
    result[tup] += 1
map(lambda t: f(t), listoftups)

defaultdict(<type 'int'>, {('G', 'H'): 1, ('A', 'B'): 2, ('C', 'D'): 2, ('E', 'F'): 1})

Solution 5:

from collections import Counter


tuples = [('A', 'B'), ('C', 'D'), ('E', 'F'), ('G', 'H'), ('A', 'B'), ('C', 'D')]

counted = Counter(tuples).most_common()

s_t = sorted(counted, key=lambda x: x[0][0])

for key, value in s_t:
    print key, value

The above code will also sort according to the value of the first string in the tuple.

Console session:

>>> from collections import Counter
>>> tuples = [('A', 'B'), ('C', 'D'), ('E', 'F'), ('G', 'H'), ('A', 'B'), ('C', 'D'), ('C', 'D'), ('C', 'D')]
>>> counted = Counter(tuples).most_common()
>>> counted
Out[8]: [(('C', 'D'), 4), (('A', 'B'), 2), (('G', 'H'), 1), (('E', 'F'), 1)]
>>> sorted_tuples = sorted(counted, key=lambda x: x[0][0])
>>> sorted_tuples
Out[10]: [(('A', 'B'), 2), (('C', 'D'), 4), (('E', 'F'), 1), (('G', 'H'), 1)]

Post a Comment for "How To Count Number Of Duplicates In A List Of Tuples?"