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
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
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
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
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
Thank you, everyone, for the answers. Very helpful and knowledge-
expanding.
--
http://mail.python.org/mailman/listinfo/python-list
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
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.