[英]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)
Hi.你好。 I found out that when
next(nums)
is executed, stopIteration
is raised.我发现执行
next(nums)
时,会引发stopIteration
。 I thought only the max value of nums
would be removed from nums
after max(nums)
was executed.我认为在执行
max(nums)
后nums
只会从nums
中删除 nums 的最大值。 But are all the values of nums
removed after max
function is executed?但是执行完
max
function 后是否所有的nums
值都被删除了? Looking forward to help!期待帮助! Thanks
谢谢
All values are removed, since you are running through the entire iterator (all values) to find the max
value.所有值都被删除,因为您正在运行整个迭代器(所有值)以找到
max
。 Which then removes all the values.然后删除所有值。
Ex:前任:
>>> 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
>>>
The reason for this is because in order to find the maximum value, you need to loop through and use all the values in the iterator.这样做的原因是因为为了找到最大值,您需要循环并使用迭代器中的所有值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.