Hell Man
Hell Man

Reputation: 873

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 list of tuples and want the output as follows:

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

How can I do this in python? I was thinking about using counter but not sure. Thank You.

Upvotes: 3

Views: 3331

Answers (5)

Nafiul Islam
Nafiul Islam

Reputation: 82440

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)]

Upvotes: 1

Guy Gavriely
Guy Gavriely

Reputation: 11396

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})

Upvotes: 1

Lucas Ribeiro
Lucas Ribeiro

Reputation: 6282

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.

Upvotes: 1

Stephen
Stephen

Reputation: 2424

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

Upvotes: 1

thefourtheye
thefourtheye

Reputation: 239443

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

Upvotes: 5

Related Questions