在 Python 中的单独一行上打印变量的每个项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19941141/
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
Printing each item of a variable on a separate line in Python
提问by MSK
I am trying to print a list in Python that contains digits and when it prints the items in the list all print on the same line.
我正在尝试用 Python 打印一个包含数字的列表,当它打印列表中的所有项目时,它们都打印在同一行上。
print ("{} ".format(ports))
here is my output
这是我的输出
[60, 89, 200]
how can I see the result in this form:
我怎样才能看到这种形式的结果:
60
89
200
I have tried print ("\n".join(ports))
but that does not work.
我试过了,print ("\n".join(ports))
但这不起作用。
回答by jramirez
ports = [60, 89, 200]
for p in ports:
print (p)
回答by Martijn Pieters
Loop over the list and print each item on a new line:
循环遍历列表并在新行上打印每个项目:
for port in ports:
print(port)
or convert your integers to strings before joining:
或在加入之前将整数转换为字符串:
print('\n'.join(map(str, ports)))
or tell print()
to use newlines as separators and pass in the list as separate arguments with the *
splat syntax:
或者告诉print()
使用换行符作为分隔符,并使用*
splat 语法将列表作为单独的参数传递:
print(*ports, sep='\n')
Demo:
演示:
>>> ports = [60, 89, 200]
>>> for port in ports:
... print(port)
...
60
89
200
>>> print('\n'.join(map(str, ports)))
60
89
200
>>> print(*ports, sep='\n')
60
89
200
回答by abarnert
i have tried print ("\n".join(ports)) but does not work.
我试过打印 ("\n".join(ports)) 但不起作用。
You're very close. The only problem is that, unlike print
, join
doesn't automatically convert things to strings; you have to do that yourself.
你很亲近。唯一的问题是,与 不同print
,join
它不会自动将事物转换为字符串;你必须自己做。
For example:
例如:
print("\n".join(map(str, ports)))
… or …
… 或者 …
print("\n".join(str(port) for port in ports))
If you don't understand either comprehensions or map
, both are equivalent* to this:
如果您不理解推导式或map
,则两者都等同于*:
ports_strs = []
for port in ports:
ports_strs.append(str(port))
print("\n".join(ports_strs))
del ports_strs
In other words, map(str, ports)
will give you the list ['60', '89', '200']
.
换句话说,map(str, ports)
会给你列表['60', '89', '200']
。
Of course it would be silly to write that out the long way; if you're going to use an explicit for
statement, you might as well just print(port)
directly each time through the loop, as in jramirez's answer.
当然,把它写得很长是愚蠢的。如果您要使用显式for
语句,则最好print(port)
每次都直接通过循环,如 jramirez 的回答。
* I'm actually cheating a bit here; both give you an iteratorover a sort of "lazy list" that doesn't actually exist with those values. But for now, we can just pretend it's a list. (And in Python 2.x, it was.)
* 我实际上在这里作弊了;两者都为您提供了一个迭代器,用于一种实际上并不存在于这些值的“懒惰列表”。但是现在,我们可以假装它是一个列表。(在 Python 2.x 中,确实如此。)
回答by abarnert
If you are on Python 3.x:
如果您使用的是 Python 3.x:
>>> ports = [60, 89, 200]
>>> print(*ports, sep="\n")
60
89
200
>>>
Otherwise, this will work:
否则,这将起作用:
>>> ports = [60, 89, 200]
>>> for p in ports:
... print p
...
60
89
200
>>>