英文:
How to use print function with for in an interactive session
问题
以下是翻译好的部分:
简单问题关于打印函数(Python 3.9.13)是什么导致了这些行为(链接到一些信息页面将不胜感激)。我有一个字符串列表,我想在交互式会话中将它们每个打印在单独的行上。
问题:您能解释这些行为,特别是是什么导致了那些"None"出现以及如何避免它?
英文:
Simple question about print function (python 3.9.13) what causes these (a link to some informative page will be appreciated). I have a list of strings and I want to print them each on separate line in an interactive session.
>>> aa = ['word 1','test 2', 'blah 3', 'ding 4']
>>> [print(x) for x in aa]
word 1
test 2
blah 3
ding 4
[None, None, None, None]
>>> (print(x) for x in aa)
<generator object <genexpr> at 0x000001D8AC975DD0>
>>> print(x) for x in aa
SyntaxError: invalid syntax
>>> {print(x) for x in aa}
word 1
test 2
blah 3
ding 4
{None}
>>>
Question: could you explain these behaviours, especially what causes those None to appear and how to avoid it?
答案1
得分: 3
None
出现在这些地方是因为 print
返回 None
(我可以尝试解释这个问题,但是StackOverflow上的其他人已经更好地解释了,比如这个答案。
>>> [print(x) for x in aa]
因为 print("foo")
的输出是 None,所以 [print(x) for x in aa]
的输出将是 [None, None, None None]
(每个 aa
元素对应一个 None
)。
>>> (print(x) for x in aa)
这创建了一个 生成器对象,稍后可以对其进行迭代:
o = (print(x) for x in aa)
for i in o:
i
# 输出是
1
2
3
4
5
>>> {print(x) for x in aa}
这与示例#1类似,但不是创建一个列表,而是创建一个集合(基本上是一个只包含唯一值的无序列表)。由于列表 [None, None, None None]
中只有一个唯一值,整个集合被简化为{None}
。
如何避免
避免这个问题的最简单方法是在类似于列表推导式的情况下将 print
放在外面。而不是 [print(x) for x in aa]
,可以使用 print([x for x in aa])
。此外,如果创建函数,请让它们返回值,然后使用 print
调用函数,而不是让函数以打印值的方式结束。
英文:
None
appears in these areas because print
returns None
(I could try to explain this, but others on StackOverflow have explained this better, such as this answer.
>>> [print(x) for x in aa]
Because the output of print("foo")
is None, the output of [print(x) for x in aa]
is going to be [None, None, None None]
, (one None
for each element of aa
.
>>> (print(x) for x in aa)
This creates a generator object, something which can later be iterated over:
o = (print(x) for x in aa)
for i in o:
i
# output is
1
2
3
4
5
>>> {print(x) for x in aa}
This is similar to example #1, except instead of creating a list, it creates a set (basically an unordered list with only unique values). Since there is only one unique value in the list [None, None, None None]
, the whole thing is reduced down to {None}
How to avoid it
The simplest way of avoiding it is to use print
outside of things similar to list comprehensions. Instead of [print(x) for x in aa]
, do print([x for x in aa])
. Also, if you create functions, have them return values, then call the function with print, instead of having the function end by printing a value.
答案2
得分: 2
[print(x) for x in aa]
, {print(x) for x in aa}
是列表和字典推导式。(print(x) for x in aa)
是生成器推导式。语法可以理解为:
对于可迭代对象 aa
中的每个元素 x
,将 (print(x)
的返回值放入列表/集合中。因此,对于每个元素 x
,执行 print(x)
,打印结果并返回 None
。你看到的是 print
导致每个元素被打印,但也看到结果列表,其中只包含 None
。
请参考 https://docs.python.org/3.9/whatsnew/2.0.html?highlight=comprehension#list-comprehensions 了解列表推导式。
你可以这样写:
for x in aa: print(x)
将其放在一行上,然后按两次回车。
英文:
[print(x) for x in aa]
, {print(x) for x in aa}
are list and dictionary comprehensions. (print(x) for x in aa)
is a generator comprehension. The syntax can be read as:
For each element x
in the iterable aa
, emplace the return value of (print(x)
into a list/set. So for each element x
, print(x)
is executed, prints the result and returns None
. You are seeing that print
resulted in each element being printed, but also see the resulting list, that only contains None
.
See https://docs.python.org/3.9/whatsnew/2.0.html?highlight=comprehension#list-comprehensions for list comprehensions
What you can do is to write
for x in aa: print(x)
on a single line and then hit enter twice
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论