开发者

python - same instruction, different outcome

开发者 https://www.devze.com 2023-03-20 11:29 出处:网络
Could someone help me understand what is going on in the following Python code (python 3.2)? I\'m really clueless here.

Could someone help me understand what is going on in the following Python code (python 3.2)? I'm really clueless here.

import sys
u = sys.stdin.readline()
   # try en开发者_高级运维tering the string "1 2 3" 
r = map(lambda t: int(t.strip()),u.split())
print(sum(r)) # prints 6
print(sum(r)) # prints 0 ?

Thank you.


map() in Python 3.x returns an iterator, not a list. Putting it through sum() the first time consumes it, leaving nothing for the second time.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号