简体   繁体   English

在单独的行中打印列表列表

[英]Print list of lists in separate lines

I have a list of lists:我有一个列表列表:

a = [[1, 3, 4], [2, 5, 7]]

I want the output in the following format:我想要以下格式的输出:

1 3 4
2 5 7

I have tried it the following way , but the outputs are not in the desired way:我已按以下方式尝试过,但输出不是所需的方式:

for i in a:
    for j in i:
        print(j, sep=' ')

Outputs:输出:

1
3
4
2
5
7

While changing the print call to use end instead:将打印调用更改为使用end

for i in a:
    for j in i:
        print(j, end = ' ')

Outputs:输出:

1 3 4 2 5 7

Any ideas?有任何想法吗?

Iterate through every sub-list in your original list and unpack it in the print call with * :遍历原始列表中的每个子列表,并在打印调用中使用*其解包:

a = [[1, 3, 4], [2, 5, 7]]
for s in a:
    print(*s)

The separation is by default set to ' ' so there's no need to explicitly provide it.默认情况下,分隔设置为' '因此无需明确提供。 This prints:这打印:

1 3 4
2 5 7

In your approach you were iterating for every element in every sub-list and printing that individually.在您的方法中,您对每个子列表中的每个元素进行迭代并单独打印。 By using print(*s) you unpack the list inside the print call, this essentially translates to:通过使用print(*s)您可以在 print 调用中解压缩列表,这基本上转换为:

print(1, 3, 4)  # for s = [1, 2, 3]
print(2, 5, 7)  # for s = [2, 5, 7]

oneliner:单线:

print('\n'.join(' '.join(map(str,sl)) for sl in l))

explanation:解释:
you can convert list into str by using join function:您可以使用 join 函数将list转换为str

l = ['1','2','3']
' '.join(l) # will give you a next string: '1 2 3'
'.'.join(l) # and it will give you '1.2.3'

so, if you want linebreaks you should use new line symbol.所以,如果你想要换行符,你应该使用换行符。
But join accepts only list of strings.但是 join 只接受字符串列表。 For converting list of things to list of strings, you can apply str function for each item in list:要将事物列表转换为字符串列表,您可以对列表中的每个项目应用str函数:

l = [1,2,3]
' '.join(map(str, l)) # will return string '1 2 3'

And we apply this construction for each sublist sl in list l我们对列表l每个子列表sl应用此构造

You can do this:你可以这样做:

>>> lst = [[1, 3, 4], [2, 5, 7]]
>>> for sublst in lst:
...     for item in sublst:
...             print item,        # note the ending ','
...     print                      # print a newline
... 
1 3 4
2 5 7
def print_list(s):
    for i in range(len(s)):
        if isinstance(s[i],list):
            k=s[i]
            print_list(k)
        else:
            print(s[i])
            
s=[[1,2,[3,4,[5,6]],7,8]]
print_list(s)

you could enter lists within lists within lists ..... and yet everything will be printed as u expect it to be.您可以在列表中的列表中输入列表......但所有内容都将按照您的预期打印。

Output:输出:

1
2
3
4
5
6
7
8

I believe this is pretty simple:我相信这很简单:

a = [[1, 3, 4], [2, 5, 7]]   # defines the list
    
for i in range(len(a)):      # runs for every "sub-list" in a
    for j in a[i]:           # gives j the value of each item in a[i]
        print(j, end=" ")    # prints j without going to a new line
    print()                  # creates a new line after each list-in-list prints

output输出

1 3 4 
2 5 7 
a = [[1, 3, 4], [2, 5, 7]]
for i in a:
    for j in i:
        print(j, end = ' ')
    print('',sep='\n')

output:输出:

1 3 4
2 5 7

There is an alternative method to display list rather than arranging them in sub-list:有一种显示列表而不是将它们排列在子列表中的替代方法:

tick_tack_display = ['1', '2', '3', '4', '5', '6', '7', '8', '9']

loop_start = 0
count = 0
while loop_start < len(tick_tack_display):
     print(tick_tack_display[loop_start], end = '')
     count +=1
     if count - 3 == 0:
       print("\n")
       count = 0
     loop_start += 1

OUTPUT :输出 :

123
456
789
lst = [[1, 3, 4], [2, 5, 7]]
 
def f(lst ):
    yield from lst


for x in f(lst):
    print(*x) 

using "yield from"...使用“收益”...

Produces Output产生输出

1 3 4
2 5 7

[Program finished] 

There's an easier one-liner way:有一种更简单的单线方式:

a = [[1, 3, 4], [2, 5, 7]] # your data
[print(*x) for x in a][0] # one-line print

And the result will be as you want it:结果将如您所愿:

1 3 4
2 5 7

Make sure you add the [0] to the end of the list comprehension, otherwise, the last line would be a list of None values equal to the length of your list.确保将[0]添加到列表推导式的末尾,否则,最后一行将是等于列表长度的None值列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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