Skip to main content
 首页 » 编程设计

Python 类型错误 : 'int' object is not iterable

2025年05月04日9三少

下面的代码给我错误 Python

类型错误:“int”对象不可迭代:

代码:

hosts = 2 
AppendMat = [] 
Mat = np.zeros((hosts,hosts)) 
Mat[1][0] = 5 
for i in hosts: 
    for j in hosts: 
        if Mat[i][j]>0 and Timer[i][j]>=5: 
            AppendMat.append(i) 

我该如何修复错误 -

TypeError: 'int' object is not iterable? 

其次,如果 if 条件为真,我如何附加 i 和 j 的值?在这里,我试图仅附加 i。

请您参考如下方法:

试试这个:

for i in xrange(hosts):