简体   繁体   中英

Print list of lists in Python in specific order

I have this list:

vshape = [['0','1','1'],['1','0','1'],['1','1','0'],['1','0','1'],['0','1','1']]

I need to print out every item in specific order - one line of vshape[0][0], vshape[1][0], vshape[2][0], vshape[3][0], and vshape[4][0]; followed by line of vshape[0][1], vshape[1][1] ans so on...

Output should look like ('0's creating a V-shape):

01110
10101
11011

Use zip :

for r in zip(*vshape):
    print(''.join(r))

# 01110
# 10101
# 11011
vshape = [['0','1','1'],['1','0','1'],['1','1','0'],['1','0','1'], ['0','1','1']]
for i in range(3):
    for j in range(5):
        print (vshape[j][i], end=' ')
    print()

This is one of my most used methods to print out patterns. In this, I use two nested for loops.

perhaps easier to understand with transpose, using numpy.

for r in np.array(vshape).T:
    print(''.join(r))

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