Re: Pythonic way to count sequences

2013-04-25 Thread Steven D'Aprano
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

Re: Pythonic way to count sequences

2013-04-25 Thread Serhiy Storchaka
25.04.13 08:26, Chris Angelico написав(ла): So you can count them up directly with a dictionary: count = {} for sequence_tuple in list_of_tuples: count[sequence_tuple] = count.get(sequence_tuple,0) + 1 Or alternatives: count = {} for sequence_tuple in list_of_tuples: if

Re: Pythonic way to count sequences

2013-04-25 Thread Denis McMahon
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

Re: Pythonic way to count sequences

2013-04-25 Thread Modulok
On 4/25/13, Denis McMahon denismfmcma...@gmail.com wrote: 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

Re: Pythonic way to count sequences

2013-04-25 Thread Matthew Gilson
A Counter is definitely the way to go about this. Just as a little more information. The below example can be simplified: from collections import Counter count = Counter(mylist) With the other example, you could have achieved the same thing (and been backward compatible to

Re: Pythonic way to count sequences

2013-04-25 Thread CM
Thank you, everyone, for the answers. Very helpful and knowledge- expanding. -- http://mail.python.org/mailman/listinfo/python-list

Pythonic way to count sequences

2013-04-24 Thread CM
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

Re: Pythonic way to count sequences

2013-04-24 Thread Chris Angelico
On Thu, Apr 25, 2013 at 3:05 PM, CM cmpyt...@gmail.com 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.