![](/img/trans.png)
[英]Why does the map object in Python 3 need to be cast to a list to function with comprehension?
[英]Why list comprehension with map() function in python can return None
>>> 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]
?
print是一個函數,但它返回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
打印i
1,2,3,4,然后每次list comprehension
返回其輸出是None
因此無,無,無,無
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.