Search code examples
pythonsplitlines

python split all lines in columns, no spaces in lines


I have seen many ways to split lines of a text file into columns, but I didn't find the way to do it with lines with no spaces.

My text file looks like:
CABDCBADBCADBC
CBDACBABCABCDA
BCDABCDABABABC
CBADCBACBADBCA

And I would like to split this into columns and then work with each column. So I should get something like:

a = [C,C,B,C]  
b = [A,B,C,B]  
c = [B,D,D,A]  
[...]

Then I would like to know how many times each letter is in each column.

It looks simple, but I didn't manage to make it and didn't find nothing similar... Do you have any ideas?


Solution

  • Assuming that all the lines are the same length and you're using Python 2 you could use izip and Counter:

    from collections import Counter
    from itertools import izip
    
    with open('test.txt') as f:
        print [(x, Counter(x)) for x in izip(*(line.strip() for line in f))]
    

    Output (partial):

    [
        (('C', 'C', 'B', 'C'), Counter({'C': 3, 'B': 1})), 
        (('A', 'B', 'C', 'B'), Counter({'B': 2, 'A': 1, 'C': 1})), 
        (('B', 'D', 'D', 'A'), Counter({'D': 2, 'A': 1, 'B': 1})),
        ...
    ]