[英]int object not iterable when concatenating a list?
def get_fail_pass_average(number_list):
for number in number_list:
under_50_list = []
over_50_list = []
if number >= 50:
over_50_list += list(number)
else:
under_50_list = under_50_list + list(number)
Hi there,你好呀,
For this code, I am trying to add a number to the over_50_list if that number if over 50. However, I get an error message stating that the int object is not interable.对于此代码,如果该数字超过 50,我将尝试向 over_50_list 添加一个数字。但是,我收到一条错误消息,指出 int object 不可交互。 But aren't I adding a list with another list?
但是我不是在添加一个列表和另一个列表吗? How can that produce an error?
那怎么会产生错误?
(I haven't included all the code here; I'm just trying to understand why I can't add these two lists. The aim of this code is to produce a list which gives the average of the over 50 and at 50 marks and under 50 marks) (我没有在这里包含所有代码;我只是想了解为什么我不能添加这两个列表。这段代码的目的是生成一个列表,给出超过 50 分和 50 分的平均值50分以下)
You problem isn't adding things to the existing list
s.您的问题是没有将东西添加到现有的
list
。 It's calling the list()
constructor on a number.它在一个数字上调用
list()
构造函数。 That doesn't work;那是行不通的;
list()
expects to receive an iterable that it converts to a list
, but numbers are not iterable. list()
期望接收一个它转换为list
的可迭代对象,但数字是不可迭代的。
You really just want the list.append
method to add single elements to the end of the list
, eg:您真的只是想要
list.append
方法将单个元素添加到list
的末尾,例如:
over_50_list.append(number)
but if you insist on making single element temporary list
s for the purpose, or really like the +=
syntax (which expects an iterable, just like list()
itself does), you could do:但是,如果您坚持为此目的制作单个元素的临时
list
,或者真的很喜欢+=
语法(它需要一个可迭代的,就像list()
本身一样),您可以这样做:
over_50_list += [number]
which makes a list
literal containing a single value, which can then be passed to +=
for concatenation (it's just less efficient when you're only adding a single value at a time, creating and destroying temporary list
s unnecessarily, which append
avoids).这使得
list
文字包含单个值,然后可以将其传递给+=
进行连接(当您一次只添加一个值时效率较低,不必要地创建和销毁临时list
s, append
避免) .
Once you've fixed that, you'll find the list
s never have more than one element;一旦你解决了这个问题,你会发现
list
永远不会有多个元素。 that's because you replace them with a new empty list
each time you loop.那是因为每次循环时都会用新的空
list
替换它们。 Move the definitions above/outside the for
loop so you only make one new list
for each, then build them element by element.将定义移到
for
循环上方/外部,这样您只需为每个定义创建一个新list
,然后逐个元素地构建它们。
Change the code to if you want to add all the numbers separately as lists:如果要将所有数字分别添加为列表,请将代码更改为:
def get_fail_pass_average(number_list):
for number in number_list:
under_50_list = []
over_50_list = []
if number >= 50:
over_50_list += [number]
else:
under_50_list += [number]
But, if you want to add the numbers to the lists directly, you can do:但是,如果您想直接将数字添加到列表中,您可以执行以下操作:
def get_fail_pass_average(number_list):
for number in number_list:
under_50_list = []
over_50_list = []
if number >= 50:
over_50_list.append(number)
else:
under_50_list.append(number])
Simply, you are not allowed to do list(int)
简单地说,你不能做
list(int)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.