Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 以单独的行打印列表列表_Python_List_Python 3.x_Printing_Nested Lists - Fatal编程技术网

Python 以单独的行打印列表列表

Python 以单独的行打印列表列表,python,list,python-3.x,printing,nested-lists,Python,List,Python 3.x,Printing,Nested Lists,我有一份清单: a = [[1, 3, 4], [2, 5, 7]] 我想要以下格式的输出: 1 3 4 2 5 7 我尝试了以下方法,但输出结果不符合要求: for i in a: for j in i: print(j, sep=' ') 产出: 1 3 4 2 5 7 将打印调用更改为使用end时: for i in a: for j in i: print(j, end = ' ') 产出: 1 3 4 2 5 7 有什么想法吗

我有一份清单:

a = [[1, 3, 4], [2, 5, 7]]
我想要以下格式的输出:

1 3 4
2 5 7
我尝试了以下方法,但输出结果不符合要求:

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

1
3
4
2
5
7
将打印调用更改为使用
end
时:

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

1 3 4 2 5 7

有什么想法吗?

反复浏览原始列表中的每个子列表,并在打印调用中使用
*
:

a = [[1, 3, 4], [2, 5, 7]]
for s in a:
    print(*s)
默认情况下,分隔设置为
'
,因此无需显式提供分隔。这张照片是:

1 3 4
2 5 7
在您的方法中,您对每个子列表中的每个元素进行迭代,并单独打印这些元素。通过使用
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))
说明:
您可以使用连接功能将
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'
因此,如果需要换行符,应使用新的换行符。
但是join只接受字符串列表。要将物品列表转换为字符串列表,可以对列表中的每个项目应用
str
函数:

l = [1,2,3]
' '.join(map(str, l)) # will return string '1 2 3'
我们将此结构应用于列表中的每个子列表
sl
l
,您可以这样做:

>>> 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
您可以在列表中的列表中输入列表。。。。。然而所有的东西都会像你期望的那样被打印出来

输出:

1
2
3
4
5
6
7
8

我相信这很简单:

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
输出

1 3 4 
2 5 7 
输出:

1 3 4
2 5 7

有一种替代方法可以显示列表,而不是将其排列在子列表中:

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
使用“收益来自”

产出

1 3 4
2 5 7

[Program finished] 

有一种更简单的单行方式:

a = [[1, 3, 4], [2, 5, 7]] # your data
[print(*x) for x in a][0] # one-line print
结果将如您所愿:

1 3 4
2 5 7

确保将
[0]
添加到列表末尾,否则,最后一行是与列表长度相等的
None
值列表。

请在您的答案中添加一些描述,并解释其工作原理和解决问题的方法。请重新阅读问题,因为此答案无法解决问题。
lst = [[1, 3, 4], [2, 5, 7]]
 
def f(lst ):
    yield from lst


for x in f(lst):
    print(*x) 
1 3 4
2 5 7

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