简体   繁体   中英

Print list of lists in matrix format

I have a list of lists:

[[15 16 18 19 12 11],[13 19 23 21 16 12],[12 15 17 19 20 10],[10 14 16 13 9  6]]

The length of each list in the list is the same . I want to print out as rows and columns such as:

15 16 18 19 12 11

13 19 23 21 16 12

12 15 17 19 20 10

10 14 16 13  9  6 

I know I can do it by using

lst = (' '.join(map(str,lst))), 

But I want every integer to indent at the same level like the 9 should be indented below the 0 of 20 , and 6 should be under 0 of 10 .

Given an input (list of lists) ll :

'\n'.join(' '.join('%2d' % x for x in l) for l in ll)

Result:

15 16 18 19 12 11
13 19 23 21 16 12
12 15 17 19 20 10
10 14 16 13  9  6

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