【问题标题】:Why do I get "TypeError: 'int' object is not iterable"为什么我会得到“TypeError:'int' object is not iterable”
【发布时间】:2019-10-18 00:26:53
【问题描述】:

我收到了TypeError: 'int' object is not iterable 代码:

a = [[1, 2, 3], [3, 10, 11], [5, 6, 7]]

b = list(zip(*a))

total = 0

for i in len(b):
    for j in len(i):
        total += b[i][j]
    total = total/len(b[i])
b[i] = total

我正在尝试获取列表中每个元组的总和,最终应该得到一个类似于:[9, 18, 21] 的列表。

为什么我在运行代码时收到此错误:TypeError: 'int' object is not iterable

【问题讨论】:

  • for i in len(b): 正在尝试迭代 len 返回的数字。 for i in 3 没有意义。我想你的意思是for i in range(len(b)):
  • FWIW,[sum(t) for t in zip(*a)]

标签: python-3.x list tuples


【解决方案1】:
a = [[1, 2, 3], [3, 10, 11], [5, 6, 7]]
b = list(zip(*a))
new_list = []
for i in b:
    total = 0 
    for j in i:
        total+=j
    new_list.append(total)

print(new_list)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 2020-03-05
    • 1970-01-01
    • 2019-04-12
    相关资源
    最近更新 更多