[Tutor] counting elements in list

Shashwat Anand anand.shashwat at gmail.com
Wed Sep 15 19:23:01 CEST 2010


On Wed, Sep 15, 2010 at 1:14 PM, kumar s <ps_python at yahoo.com> wrote:

> 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.
>

>>> 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']
>>> "\t".join(x+":"+str(k.count(x)) for x in 'ATGC')
'A:0\tT:23\tG:0\tC:6'



>
>
>
>
> >>> 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
>
>
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
~l0nwlf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100915/bafe3a35/attachment-0001.html>


More information about the Tutor mailing list