列表理解返回值加上 [None, None, None],为什么? [英] List comprehension returning values plus [None, None, None], why?

查看:64
本文介绍了列表理解返回值加上 [None, None, None],为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习理解.我得到了 print(x) 部分(我认为.它打印了通过in"测试的 x 的值)但为什么它之后还返回一个 None 列表?

<预><代码>>>>G['a', 'x', 'p']>>>[print(x) for x in g]一种X磷[无,无,无]#这是什么?

解决方案

您使用列表推导式打印列表中的项目,然后打印列表本身.尝试将列表分配给变量.

<预><代码>>>>G['a', 'x', 'p']>>>x = [print(x) for x in g]一种X磷#

现在列表在 x 中并且没有打印.名单还在……

<预><代码>>>>打印(x)[无,无,无]>>>X[无,无,无]

Im studying comprehensions. I get the print(x) part (i think. It prints the value of x that passes the 'in' test) but why is it also returning a list of None afterward?

>>> g
['a', 'x', 'p']

>>> [print(x) for x in g]
a
x
p
[None, None, None] #whats this? 

解决方案

You use a list comprehension to print the items in the list, and then the list itself is printed. Try assigning the list to a variable instead.

>>> g
['a', 'x', 'p']

>>> x = [print(x) for x in g]
a
x
p
#

Now the list is in x and isnt printed. The list is still there...

>>> print(x)
[None, None, None]
>>> x
[None, None, None]

这篇关于列表理解返回值加上 [None, None, None],为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆