![](/img/trans.png)
[英]What happens to a immutable object in python when its value is changed?
[英]What happens after getting max value of map object in Python
nums = map(int, input().split())
if max(nums)** 2 == next(nums)**2 + next(nums)**2:
print(True)
你好。 我发现执行next(nums)
时,会引发stopIteration
。 我认为在执行max(nums)
后nums
只会从nums
中删除 nums 的最大值。 但是执行完max
function 后是否所有的nums
值都被删除了? 期待帮助! 谢谢
所有值都被删除,因为您正在运行整个迭代器(所有值)以找到max
。 然后删除所有值。
前任:
>>> it = iter([1, 2, 3])
>>> max(it)
3
>>> next(it)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
next(it)
StopIteration
>>>
这样做的原因是因为为了找到最大值,您需要循环并使用迭代器中的所有值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.