简体   繁体   English

在 Python 中获得 map object 的最大值后会发生什么

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM