C
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 names and set them all to 0
alpha = 0
beta = 0
delta = 0
gamma = 0
# etc...
# 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.
# Finally, I need a list created like this:
result_list = [alpha, beta, delta, gamma] #etc...in that order
I can sense there is very likely an elegant/Pythonic way to do this,
and probably with a dict, or possibly with some Python structure I
don't typically use. Suggestions sought. Thanks.
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...
# 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.
# Finally, I need a list created like this:
result_list = [alpha, beta, delta, gamma] #etc...in that order
I can sense there is very likely an elegant/Pythonic way to do this,
and probably with a dict, or possibly with some Python structure I
don't typically use. Suggestions sought. Thanks.