Pythonic way to count sequences

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Apr 25 02:14:16 EDT 2013


On Wed, 24 Apr 2013 22:05:52 -0700, CM wrote:

> I have to count the number of various two-digit sequences in a list such
> as this:
> 
> mylist = [(2,4), (2,4), (3,4), (4,5), (2,1)]  # (Here the (2,4) sequence
> appears 2 times.)
> 
> and tally up the results, assigning each to a variable.  The inelegant
> first pass at this was something like...
> 
> # Create names and set them all to 0
> alpha = 0
> beta = 0
> delta = 0
> gamma = 0
> # etc...

Do they absolutely have to be global variables like that? Seems like a 
bad design, especially if you don't know in advance exactly how many 
there are.


> # loop over all the tuple sequences and increment appropriately for
> sequence_tuple in list_of_tuples:
>     if sequence_tuple == (1,2):
>         alpha += 1
>     if sequence_tuple == (2,4):
>         beta += 1
>     if sequence_tuple == (2,5):
>         delta +=1
> # etc... But I actually have more than 10 sequence types.

counts = {}
for t in list_of_tuples:
    counts[t] = counts.get(t, 0) + 1


Or, use collections.Counter:

from collections import Counter
counts = Counter(list_of_tuples)


> # Finally, I need a list created like this: result_list = [alpha, beta,
> delta, gamma] #etc...in that order

Dicts are unordered, so getting the results in a specific order will be a 
bit tricky. You could do this:

results = sorted(counts.items(), key=lambda t: t[0])
results = [t[1] for t in results]

if you are lucky enough to have the desired order match the natural order 
of the tuples. Otherwise:

desired_order = [(2, 3), (3, 1), (1, 2), ...]
results = [counts.get(t, 0) for t in desired_order]



-- 
Steven



More information about the Python-list mailing list