首页 > 解决方案 > TypeError:“int”对象不可在 for 循环中使用 max 函数进行迭代

问题描述

这是我的代码的一部分:

if click(pt, rec):
            entrytxt = eninput.getText() 
            
            if entrytxt in route: # route dict
                shapeID = route[entrytxt] 
            for i in shapeID:
                coordinates = shape[i] # shape dict
                print(len(coordinates))
----------------------------------------------------------------------------

ourput for len(coordinates):

180
349
339
184
102
405
185

desired output:
405

我试图从坐标中选择最长的列表,

并使用该索引从坐标中获取最长的列表。

坐标包含许多具有协调值的列表。(不是列表列表)

我怎样才能只打印出 405?

标签: python

解决方案


我怎么能解决这个问题?

通过不打电话len?还是通过len用作键功能?

您并没有真正解释什么coordinates是或包含什么。你写的当然没有任何意义:len()返回一个整数,所以:

ourput for print(len(coordinates)):

180
349
339
184
102
405
185

实际上不可能。当使用单个参数调用时,含义max(len(coordinates))也不能max,它必须是可迭代的,(正如错误消息告诉您的那样)整数不是。


推荐阅读