简体   繁体   中英

Loop print through two lists to get two columns in Python

I am trying to print my data in two columns. The data which I have

print(summary_data) 
[('105689_S2', 9477442, 488850, 563029), ('WT101589_S1', 9849916, 507676, 584819)]

I am able to print my data through a loop:

for data in summary_data:
    print 'Name                      ', data[0]
    print 'Calculations1', data[0], ':    ', data[1]
    print 'Calculations2', data[0], ':    ', data[2]
    print 'Calculations3', data[0], ':    ', data[3]
    print '-------------------------------------------'

Which provides:

Sample name         105689_S2
Calculations1 :     9477442
Calculations2 :     488850
Calculations3 :     563029
-------------------------------------------
Sample name         WT101589_S1
Calculations1 :     9849916
Calculations2 :     507676
Calculations3 :     584819
-------------------------------------------

I would like my output to be formatted like:

Sample name         105689_S2   WT101589_S1
Calculations1 :     9477442   9849916
Calculations2 :     488850   507676
Calculations3 :     563029   584819
-------------------------------------------

I have tried

zipped = ([list(x) for x in zip(*summary_data)])
for data in zipped:
    print data[0]

Which gives:

105689_S2
909172
31908
31360

But now I am lost on how I can get the two column's printed..Could someone help me?

You can use zip() like this:

summary_data = [('105689_S2', 9477442, 488850, 563029), ('WT101589_S1', 9849916, 507676, 584819)]
titles = ['Sample name:', 'Calculations1:', 'Calculations2:', 'Calculations3:']

for item in zip(titles, *summary_data):
    print '{}\t{}\t{}'.format(*item)

Output:

Sample name:     105689_S2       WT101589_S1
Calculations1:   9477442 9849916
Calculations2:   488850  507676
Calculations3:   563029  584819

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