简体   繁体   中英

convert columns of text to rows in python 3x

input text image

I am using the below code to convert the columns to rows in the text. My requirement is to find the count of each character in each column in the text

b=[''.join(i) for i in zip(*a2.split())]
print(b)

I am getting below input

['CCACTCGT', 'GTGGCCCC', 'AGCACTGC', 'CCTGCAGA', 'TTTAACCA', 'CGTACCTC', 'CACCCCCA', 'CGCCCCTT', 'GCTCCATG', 'CCAAAGGA', 'GCTCGCCT', 'ACTCACCC', 'ATCCTGGG', 'GGAACGCT', 'ACATCCTG', 'CGGCTTGC', 'TCAACCCG', 'TACGCGTT', 'GTCATCGT', 'ACAGAACC', 'CCCCCCTC', 'CACCCTGT', 'CACTTCCG', 'CGACTTCC', 'AGCCTCGA', 'AACCTGCA', 'ACTTCGTG', 'GCCTTCGT', 'CCTCGTCG', 'TTGCGGTC', 'CTGAGTGA', 'GCTCGGTG', 'GTACACGC', 'GCCTGCGT', 'CGCCAGCG', 'GGATCGTA', 'CAGGCGGG', 'ATACCGCG', 'CCTTCGTC', 'CCCCTGAC', 'CGTCCCGC', 'CGCTAGTC', 'CGGCGCGG', 'CACCCCCC', 'TGCGCGTC', 'GACTCCGC', 'CCATCCAC', 'AGTCTTCG', 'CGCTGCGC', 'AATCTCCC', 'CACCACCC', 'TTGCGCTA', 'TCGTGCGC', 'CTTGGAGA', 'CGTAGTCG', 'CTTGCGCC', 'CCTAGCGC', 'ATTGGCGC', 'CCTCGGCC', 'TACCGCCG', 'CGCTCCGC', 'TAGCCTGC', 'CCTATTCC', 'ACAACCCA', 'GTGCCGGC']

You can see the last 5 columns in the text are not coming in the list. Iam not able to figure it out why this is happening.Any help would be highly appreciated. Also please suggest if there is any other way to achieve the same result.

zip returns as many tuples as there are items in the shortest iterable, so only full columns are returned. To get all columns you can use zip_longest , like this:

from itertools import zip_longest
b = [''.join(i) for i in zip_longest(*a2.split(), fillvalue='')]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM