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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43425270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:02:54  来源:igfitidea点击:

TypeError: 'int' object is not iterable

pythontypeerror

提问by Austin Lee

There is an error when I execute This code-
for i in len(str_list): TypeError: 'int' object is not iterable

执行此代码时出错 -
for i in len(str_list): TypeError: 'int' object is not iterable

How would I fix it? (Python 3)

我该如何解决?(蟒蛇 3)

def str_avg(str):
    str_list=str.split()
    str_sum=0
    for i in len(str_list):
        str_sum += len(str_list[i])
    return str_sum/i

回答by Martijn Pieters

You are trying to loop over in integer; len()returns one.

您正在尝试以整数循环;len()返回一个。

If you must produce a loop over a sequence of integers, use a range()object:

如果必须在整数序列上生成循环,请使用range()对象

for i in range(len(str_list)):
    # ...

By passing in the len(str_list)result to range(), you get a sequence from zero to the length of str_list, minus one (as the end value is not included).

通过将len(str_list)结果传递给range(),您将获得一个从 0 到 长度str_list减 1的序列(因为不包括最终值)。

Note that now your ivalue will be the incorrectvalue to use to calculate an average, because it is one smallerthan the actual list length! You want to divide by len(str_list):

请注意,现在您的i值将是用于计算平均值的错误值,因为它比实际列表长度1!你想除以len(str_list)

return str_sum / len(str_list)

However, there is no needto do this in Python. You loop over the elements of the list itself. That removes the need to create an index first:

但是,无需在 Python 中执行此操作。您遍历列表本身的元素。这消除了首先创建索引的需要:

for elem in str_list
    str_sum += len(elem)

return str_sum / len(str_list)

All this can be expressed in one line with the sum()function, by the way:

所有这一切都可以用sum()函数在一行中表达,顺便说一句:

def str_avg(s):
    str_list = s.split()
    return sum(len(w) for w in str_list) / len(str_list)

I replaced the name strwith s; better not mask the built-in type name, that could lead to confusing errors later on.

换成我的名字strs; 最好不要屏蔽内置类型名称,这可能会导致稍后出现混淆错误。

回答by Aklys

For loops requires multiple items to iterate through like a list of [1, 2, 3] (contains 3 items/elements).

For 循环需要多个项目来迭代,就像一个 [1, 2, 3] 列表(包含 3 个项目/元素)。

The len function returns a single item which is an integer of the length of the object you have given it as a parameter.

len 函数返回一个项目,它是您作为参数提供的对象长度的整数。

To have something iterate as many times as the length of an object you can provide the len functions result to a range function. This creates an iterable allowing you to iterate as any times as the length of the object you wanted.

要让某个对象的迭代次数与对象的长度一样多,您可以将 len 函数结果提供给 range 函数。这将创建一个可迭代对象,允许您根据所需对象的长度进行迭代。

So do something like

所以做类似的事情

for i in range(len(str_list)):

unless you want to go through the list and not the length of the list. You can then just iterate with

除非你想通过列表而不是列表的长度。然后你可以迭代

for i in str_list:

回答by pkuphy

def str_avg(str):
    str_list = str.split()
    str_sum = len(''.join(str_list))  # get the total number of characters in str
    str_count = len(str_list)  # get the total words

    return (str_sum / str_count)