frequency of letters in column python

后端 未结 2 1540
感情败类
感情败类 2021-01-22 09:12

I want to calculate the frequency of occurrence of each letter in all columns: for example I have this three sequences :

seq1=AATC
seq2=GCCT
seq3=ATCA
<         


        
2条回答
  •  南方客
    南方客 (楼主)
    2021-01-22 09:31

    As with my answer to your last question, you should wrap your functionality in a function:

    def lettercount(pos):
        return {c: pos.count(c) for c in pos}
    

    Then you can easily apply it to the tuples from zip:

    counts = [lettercount(t) for t in zip(seq1, seq2, seq3)]
    

    Or combine it into the existing loop:

    ...
    counts = []
    for position in zip(seq1, seq2, seq3): # sets at same position
        counts.append(lettercount(position))
        for pair in combinations(position, 2): # pairs within set
            ...
    

提交回复
热议问题