[Tutor] counting elements in list
kumar s
ps_python at yahoo.com
Wed Sep 15 19:14:02 CEST 2010
Hi group:
I have a list:
k = ['T', 'C', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'C', 'T', 'T', 'T', 'C', 'T',
'T', 'T', 'C', 'C', 'T', 'T', 'T', 'C', 'T', 'T', 'T', 'T', 'T', 'T']
the allowed elements are A or T or G or C. List can have any number of A or T or
G or C
My aim is to get a string ouput with counts of each type A or T or G or C.
A:0\tT:23\tG:0\tC:6
from the above example, I could count T and C and since there are no A and G, I
want to print 0 for them. I just dont know how this can be done.
>>> d = {}
>>> for i in set(k):
... d[i] = k.count(i)
...
>>> d
{'C': 6, 'T': 23}
>>> for keys,values in d.items():
... print keys+'\t'+str(d[keys])
...
C 6
T 23
the other way i tried is:
>>> k.count('A'),k.count('T'),k.count('G'),k.count('C')
(0, 23, 0, 6)
how can I get counts for those elements not represented in list and print
them. appreciate your help.
thanks
kumar
More information about the Tutor
mailing list