python:在列表中为我打印我
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13967735/
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
python: print i for i in list
提问by Rain Zeng
When I printed a list, I got a syntax error when using this method:
当我打印列表时,使用此方法时出现语法错误:
print i for i in [1,2,3]
I knew this is ok when using this method:
使用这种方法时,我知道这是可以的:
for i in [1, 2, 3]:
print i
and I knew that
我知道
(i for i in [1, 2, 3])
is a generator object, but I just don't get it that why
是一个生成器对象,但我不明白为什么
print i for i in [1, 2, 3]
does't work. Can anyone give me a clue?
不起作用。谁能给我一个线索?
采纳答案by Aaron Digulla
In Python 2, printis a statement, not an expression or a function, so you can't use it directly in a comprehension. Use this trick:
在 Python 2 中,print是语句,而不是表达式或函数,因此您不能直接在推导式中使用它。使用这个技巧:
def f(x): print x
[f(i) for i in [1,2,3]]
Note that (f(i)...)doesn't work because this just creates a generator which would call f()if you iterated over it. The list comprehension []actually invokes f().
请注意,(f(i)...)这不起作用,因为这只会创建一个生成器,f()如果您对其进行迭代,它将调用。列表理解[]实际上调用f().
[EDIT]If you use Python > 2.6, you can achieve the same using
[编辑]如果您使用 Python > 2.6,则可以使用
from __future__ import print_function
[print(i) for i in [1, 2, 3]]
Note the ()around the argument to print.
注意()参数周围的print.
回答by BrenBarn
The list comprehension syntax x for x in ...requiresbrackets around it. That's a syntactic rule of Python, just like requiring indentation, or requiring a colon after ifor whatever. i for i in [1, 2, 3]by itself is not valid syntax. You need either [i for i in [1, 2, 3]](for a list comprehension) or (i for i in [1, 2, 3])(for a generator comprehension).
列表理解语法x for x in ...需要用括号括起来。这是 Python 的句法规则,就像要求缩进,或者要求后面有冒号一样if。 i for i in [1, 2, 3]本身不是有效的语法。您需要[i for i in [1, 2, 3]](对于列表理解)或(i for i in [1, 2, 3])(对于生成器理解)。
回答by Chris Morgan
As an expression(in the grammar):
作为expression(在语法中):
[i for i in [1, 2, 3]]is a list comprehension.(i for i in [1, 2, 3])is a generator expression.- But
i for i in [1, 2, 3]by itself is a syntax error, and that's just the way it is. There must be something surrounding it. Unless you have(or[around it, it's not a validexpression, because theforkeyword is not valid at that point.
[i for i in [1, 2, 3]]是一个列表理解。(i for i in [1, 2, 3])是一个生成器表达式。- 但
i for i in [1, 2, 3]它本身就是一个语法错误,这就是它的方式。一定有什么东西围绕着它。除非您拥有(或[围绕它,否则它不是有效的expression,因为此时for关键字无效。
Inside the print statement, it wants an expression.
在print 语句中,它需要一个expression.
(As a red herring, func(i for i in [1, 2, 3])is permitted as an expression, being a function call with the first argument being a generator expression.)
(作为红鲱鱼,func(i for i in [1, 2, 3])允许作为表达式,作为第一个参数是生成器表达式的函数调用。)
回答by Sylvain Defresne
This is invalid python syntax. The i for i in [1, 2, 3]is only valid in a list or generator comprehension, ie. surrounded by []or ()respectively.
这是无效的 python 语法。在i for i in [1, 2, 3]仅在列表或发电机的理解,即有效。被[]或()分别包围。
You'll want to use:
你会想要使用:
print '\n'.join(str(i) for i in [1, 2, 3])
回答by Martijn Pieters
The list comprehension syntax ([expression for loop]) is a shorthand loop syntax for producing a list.
列表推导式语法 ( [expression for loop]) 是用于生成列表的速记循环语法。
You are not producing a list, you want to print items in a loop. Since you are not producing a python list, you have to use a regular loop.
您不是在生成列表,而是想循环打印项目。由于您没有生成 python 列表,因此必须使用常规循环。
Alternatively, since all you are doing is printing the items on separate lines, just add the newlines yourself:
或者,由于您所做的只是在单独的行上打印项目,只需自己添加换行符:
print '\n'.join(i for i in [1, 2, 3])
This produces the same output as:
这产生与以下相同的输出:
for i in [1, 2, 3]:
print i
If you use Python 3, or use from __future__ import printat the top of your module and so use the print()function, you can send all values to the function in one call, and tell print()to use newlines in between:
如果您使用 Python 3,或者from __future__ import print在模块顶部使用,因此使用print()function,您可以在一次调用中将所有值发送到该函数,并告诉print()在两者之间使用换行符:
values = [1, 2, 3]
print(*values, sep="\n")
回答by root
Print is not a function, it's a statement, and you can't have them in expressions. Just use a regular loop as you don't want to produce a list, that a list comprehension does. In theory you can do (not that you should. at all):
Print 不是一个函数,它是一个语句,你不能在表达式中使用它们。只需使用常规循环,因为您不想生成列表,而列表理解会这样做。从理论上讲,您可以这样做(根本不应该这样做):
from __future__ import print_function
[print(my_item) for my_item in [1,2,3,4]]
1
2
3
4
Out[26]:
[None, None, None, None]

