madman
madman

Reputation: 153

Counting Unique Strings in Tuples, Python

I am trying to count unique strings in a tuple and output only the unique string and its count in a list. I was trying to use list comp., but having some issues:

def tupleTag(inputList):
    from collections import Counter

    c = Counter(inputList[0] for inputList in inputList)
    print set(inputList[0], c)

inputList = [('love','family'),('dinner','family','nomnom'),('wedding','romance','love')]
tupleTag(inputList)      

The correct output would be = 

[(love,2), (family,2)]

Upvotes: 0

Views: 393

Answers (2)

FMc
FMc

Reputation: 42421

The answer from mgilson is very slick and worth studying. Here's the garden-variety approach using comprehensions:

tups   = ...  # Your data.
c      = Counter(x for tup in tups for x in tup)
result = [(k, n) for k, n in c.items() if n > 1]

Upvotes: 1

mgilson
mgilson

Reputation: 310267

You seem to be on the right track with collections.Counter. I'd also throw in an itertools.chain:

items = itertools.chain.from_iterable(inputList)
counts = collections.Counter(items)

Now you have a map of items to the number of times they appear. If you want a sorted list of tuples of the form (<key>, <count>), you can use the most_common method -- And of course, you can filter this to find only strings which are duplicated and their counts:

repeated_items = [(key, count) for key, count in counts.most_common() if count > 1]

Note, you don't actually need the most_common to filter the items, but it does give you the results in order of how common the are. If that isn't necessary, a simple loop over the items would be more efficient:

repeated_items = [(key, count) for key, count in counts.items() if count > 1]

Upvotes: 2

Related Questions