以单独的行打印列表列表

时间:2022-01-14 15:41:35

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?

4 个解决方案

#1


8  

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(1, 3, 4)  # for s = [1, 2, 3]
print(2, 5, 7)  # for s = [2, 5, 7]

#2


9  

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. For converting list of things to list of strings, you can apply str function for each item in list:

所以,如果你想要换行,你应该使用新的线符号。但是join只接受字符串列表。要将事物列表转换为字符串列表,可以对列表中的每个项应用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应用此构造

#3


2  

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

#4


0  

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.

你可以在列表中的列表中输入列表.....然而一切都将按照你期望的那样打印出来。

#1


8  

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(1, 3, 4)  # for s = [1, 2, 3]
print(2, 5, 7)  # for s = [2, 5, 7]

#2


9  

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. For converting list of things to list of strings, you can apply str function for each item in list:

所以,如果你想要换行,你应该使用新的线符号。但是join只接受字符串列表。要将事物列表转换为字符串列表,可以对列表中的每个项应用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应用此构造

#3


2  

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

#4


0  

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.

你可以在列表中的列表中输入列表.....然而一切都将按照你期望的那样打印出来。