[英]List comprehension - TypeError: 'int' object is not iterable
I am using list comprehension to generate a list of products of two values, and am seeing a TypeError.我正在使用列表推导生成两个值的产品列表,并且看到了 TypeError。 A simplified example is below.
下面是一个简化的例子。
def exp_n(n):
k = 5
s = 1
a = [5*max(x+s-K) for x in range(0,n)]
print(a)
exp_n(10)
TypeError: 'int' object is not iterable
From looking at other posts, it seems like this usually has to do with the iterable defined in the forloop returing a non-iterable.从查看其他帖子来看,这似乎通常与在 forloop 中定义的可迭代对象返回不可迭代对象有关。 But here,
range(0,n)
is certainly iterable.但是在这里,
range(0,n)
肯定是可迭代的。 Any ideas what the issue is?任何想法是什么问题?
Base on the code I assume this is what you are looking for:根据我认为这是您正在寻找的代码:
def exp_n(n):
k = 5
s = 1
a = 5*max([(x+s-k) for x in range(0,n)])
print(a)
exp_n(10)
a = [5*max([x+s-K]) for x in range(0,n)]
? ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.