With the help of the Counter (collections module), the number of each type of characters was calculated by "columns" in the list with elements, each element is a string of letters and symbols (in my case the alternation of "a", "c", "g", "t" , "-"). Each line has 200 characters.

From this

['agtcgtcgatcgatcgatcga----', 'aaagggtctgcgatgcgaattagca', 'gcgatcgtggcg-----cgggcggg'] lst = ['agtcgtcgatcgatcgatcga----', 'aaagggtctgcgatgcgaattagca','gcgatcgtggcg-----cgggcggg'] r = [collections.Counter(ar) for ar in zip(*lst)] Print ('\n'.join(str(value) fro value in r)) 

Got it (set quantity randomly):

 Counter ({'a':2, '-':1, 'c':3}) Counter ({'t':3, '-':1, 'g':1}) Counter ({'a':2, '-':31, 'c':3}) и тд. 

And I need to get something like this (the number is also randomly set):

 1: a=2, g=1, c=0, t=0, -=0 2: a=1, g=1, c=1, t=0, -=0 3: a=0, g=0, c=3, t=0, -=0 ... 200. a=2, g=1, c=0, t=0, -=4 

Or at least this way (the main thing is to see the position number at the beginning):

 1. ({'a':2, '-':1, 'c':3}) 2. ({'t':3, '-':1, 'g':1}) 3. ({'a':2, '-':31, 'c':3}) и тд. 
  • It is not clear what should work. For example, what does a=2 mean in the first line of the desired result? - Enikeyschik
  • You can change the title of the question so that it fits the task and that it can help others with a similar task to find this question? - MaxU

2 answers 2

You can set a list of fields and make a result of them:

 lst = ['agtcgtcgatcgatcgatcga----', 'aaagggtctgcgatgcgaattagca', 'gcgatcgtggcg-----cgggcggg'] from collections import Counter rows = [Counter(ar) for ar in zip(*lst)] for i, row in enumerate(rows, 1): row_values = [row.get(x, 0) for x in 'agct-'] print('{}: a={}, g={}, c={}, t={}, -={}'.format(i, *row_values)) 

Console:

 1: a=2, g=1, c=0, t=0, -=0 2: a=1, g=1, c=1, t=0, -=0 3: a=1, g=1, c=0, t=1, -=0 4: a=1, g=1, c=1, t=0, -=0 5: a=0, g=2, c=0, t=1, -=0 ... 

You can pause and make the output format flexible:

 ... FIELDS = 'agct-' for i, row in enumerate(rows, 1): row_values = [row.get(x, 0) for x in FIELDS] row_text = ', '.join('{}={}'.format(name, value) for name, value in zip(FIELDS, row_values)) print('{}: {}'.format(i, row_text)) 
     lst = 'agtcgtcgatcgatcgatcga----',\ 'aaagggtctgcgatgcgaattagca',\ 'gcgatcgtggcg-----cgggcggg' r = [collections.Counter(ar) for ar in zip(*lst)] for n, cntr in enumerate(r, 1): print(n, dict(cntr)) # вывод номера (начиная с 1) и словаря