逐行打印嵌套列表 - Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30521975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print a nested list line by line - Python
提问by quarters
A = [[1, 2, 3], [2, 3, 4], [4, 5, 6]]
A = [[1, 2, 3], [2, 3, 4], [4, 5, 6]]
I am trying my best to print A
of the form:
我正在尽力打印A
表格:
1 2 3
2 3 4
4 5 6
That is in different lines, but I am unable to do so without all the elements in different lines. This is my code so far:
那是在不同的行中,但是如果没有不同行中的所有元素,我将无法这样做。到目前为止,这是我的代码:
for r in A:
for t in r:
print(t,)
print
This is my output:
这是我的输出:
1
2
3
2
3
4
4
5
6
It seems really simple, and I think a minor change would do it. Thanks!
看起来真的很简单,我认为稍微改变一下就可以了。谢谢!
采纳答案by James Mills
Use a simple for loop and " ".join()
mapping each int
in the nested list to a str
with map()
.
使用简单的 for 循环并将嵌套列表中的" ".join()
每个都映射int
到str
with map()
。
Example:
例子:
>>> ys = [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
>>> for xs in ys:
... print(" ".join(map(str, xs)))
...
1 2 3
4 5 6
7 8 9 10
The difference here is that we can support arbitrary lengths of inner lists.
这里的区别在于我们可以支持任意长度的内部列表。
The reason your example did not work as expected is because your inner loop is iterating over each element of the sub-list;
您的示例未按预期工作的原因是您的内部循环正在迭代子列表的每个元素;
for r in A: # r = [1, 2, 3]
for t in r: # t = 1 (on first iteration)
print(t,)
print
And print()
by default prints new-line characters at the end unless you use: print(end="")
I believe if you were using Python 2.x print t,
would work. For example:
并且print()
默认情况下在末尾打印换行符,除非您使用:print(end="")
我相信如果您使用的是 Python 2.xprint t,
会工作。例如:
>>> ys = [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
>>> for xs in ys:
... for x in xs:
... print x,
... print
...
1 2 3
4 5 6
7 8 9 10
But print(x,)
would not work as you intended it; Python 2.x or 3.x
但print(x,)
不会如你所愿;Python 2.x 或 3.x
回答by dbliss
for r in A:
print '%d %d %d' % tuple(r)
回答by lanpa
print
without trailing comma will print a newline character.
print
没有尾随逗号将打印一个换行符。
for r in A:
for t in r:
print t,
print
回答by Shashank
If you are in Python 3.x:
如果您使用的是 Python 3.x:
print(*('{} {} {}'.format(*r) for r in A), sep='\n')
or:
或者:
print(*('%d %d %d' % tuple(r) for r in A), sep='\n')
If not, you can import Python 3.x's print function from the __future__
module.
如果没有,您可以从__future__
模块中导入 Python 3.x 的打印功能。
回答by Rahul Gupta
Method-1 :
方法一:
We can use list comprehension and .join()
operator.
我们可以使用列表理解和.join()
运算符。
>>> my_list = [[1, 2, 3], [2, 3, 4], [4, 5, 6]]
>>> for item in my_list:
print ' '.join(str(x) for x in item)
1 2 3
2 3 4
4 5 6
Method-2 :
方法2:
>>> my_list = [[1, 2, 3], [2, 3, 4], [4, 5, 6]]
>>> for item in my_list:
for x in item:
print x,
print
1 2 3
2 3 4
4 5 6
Inner print
with a comma ensures that inner list's elements are printed in a single line.
Outer print
ensures that for the next inner list, it prints in next line.
内print
用逗号确保内部列表的元素在一行打印。外部print
确保对于下一个内部列表,它在下一行打印。
回答by J.S.
The same question was answered by Jim Fasarakis Hilliard in Print list of lists in separate lines.
Jim Fasarakis Hilliard 在Print list of lists indifferent lines 中回答了同样的问题。
In Python 3.x we can use:
在 Python 3.x 中,我们可以使用:
A = [[1, 2, 3], [2, 3, 4], [4, 5, 6]]
for i in A:
print(*i)
And the corresponding output is:
相应的输出是:
1 2 3
2 3 4
4 5 6
回答by Kartik Kapgate
for [a,b,c] in A:
print(a,b,c)
This might help. But if you have more number of elements in the lists or if elements in the nested lists are variable,This won't work. Below code will print all elements in a nested line in single line.
这可能会有所帮助。但是如果列表中有更多的元素或者嵌套列表中的元素是可变的,这将不起作用。下面的代码将在一行中打印嵌套行中的所有元素。
for b in A:
for p in b:
print(p,end=" ")
print()