Thank you, everyone, for the answers. Very helpful and knowledge-
expanding.
--
http://mail.python.org/mailman/listinfo/python-list
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 python2.
On 4/25/13, Denis McMahon 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 the resu
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 in
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 sequence_tup
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 in
On Thu, Apr 25, 2013 at 3:05 PM, 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.
You can use