2024-04-25 12:58:07 发布
网友
>>> counters = [1,2,3,4] >>> >>> [print(i, end=', ') for i in map(inc, counters)] 4, 5, 6, 7, [None, None, None, None]
为什么这段代码要打印[None, None, None, None]?在
[None, None, None, None]
print是一个函数,但它返回None这就是为什么您没有得到任何结果
None
做这个
In [3]: [i for i in map(abs, counters)] Out[3]: [1, 2, 3, 4] In [7]: a = print(1234) 1234 In [11]: print(a) None [print(i, end=', ') for i in map(inc, counters)]
所以当你这样做的时候print打印i1,2,3,4,,然后每次list comprehension都返回None,因此无、无、无、无
print
i
list comprehension
因为print返回{}?在
因此,打印完成(4, 5, 6, 7,),然后返回列表理解([None, None, None, None])
4, 5, 6, 7,
您要做的是使用join:
>>> ', '.join(str(i) for i in map(inc, counters)) '4, 5, 6, 7'
或者使用^{} argument of print(我现在没有Python3,但是类似这样的东西:)
print是一个函数,但它返回
None
这就是为什么您没有得到任何结果做这个
所以当你这样做的时候
print
打印i
1,2,3,4,,然后每次list comprehension
都返回None
,因此无、无、无、无因为}?在
print
返回{因此,打印完成(
4, 5, 6, 7,
),然后返回列表理解([None, None, None, None]
)您要做的是使用join:
或者使用^{} argument of print (我现在没有Python3,但是类似这样的东西:)
^{pr2}$相关问题 更多 >
编程相关推荐