int 对象不可迭代 python
int object is not iterable python
我正在尝试学习 python reduce 函数。
这是一些对我来说没有意义的代码
>>> x = [1,2,3]
>>> reduce(sum, [b for b in x if b > 0])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> reduce(sum, [b for b in x if b > 2])
3
为什么当 b > 2 而不是 b > 0 时有效
代码看起来几乎一模一样
在第二种情况下,您使用列表理解创建的列表只有一个元素,因此不会减少。这种归约不会失败,因为那里没有任何东西可以归约,所以函数 sum 不会执行,也不会失败。结果是之前存在的唯一整数。
函数 sum 在 documentation 中定义。如您所见,它应该对某些可迭代容器(例如列表或元组)中的值求和。你可以写 sum([1,2,3])
,它会像你想要的减少一样工作。如果你想通过使用缩减函数来实现这个结果,你必须确保你有一个函数,它接受两个整数和 returns 它们的总和。
这里有这样的功能:
def sum2(x, y):
return x + y
用这样的函数减少应该会给你预期的结果。
sum2 = lambda x, y: x+y
下面的函数做同样的事情,但由于 lambda 表示法,它更短更好,如果您还不知道的话,将来某个时候看看它可能会很好。
来自docs 关于reduce
:
Apply function of two arguments cumulatively to the items of iterable,
from left to right, so as to reduce the iterable to a single value
所以在第一种情况下,你错误地使用了 reduce
,因为你使用了内置的 sum
,它不接受两个参数,而是一个 iterable
。 sum
在 reduce
和 returns 和 int
的第一次迭代中消耗了可迭代对象,它不能进一步 reduceable.
在第二种情况下,sum
甚至没有在可迭代对象上调用:
If initializer is not given and iterable contains only one item, the
first item is returned.
由于iterable属于length 1
,减少returns iterable中的一项,sum
并没有什么。
reduce
函数接收一个函数和一个可迭代对象。
然后它将可迭代对象的每个元素映射到函数。
您的第一个 b > 0
函数正在尝试使用 2 个参数 运行 sum
。一个参数是累计总数,另一个是输入列表的一个元素,例如它看起来像:sum(0, 1)
.
这在文档中说明为:
Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value. ... The left argument, is the accumulated value and the right argument, is the update value from the iterable.
您的第二个 b > 2
函数所做的只是 运行 使用包含一项的迭代器来简化 reduce 函数,它不会尝试应用 sum
,如文档中所述.
If initializer is not given and iterable contains only one item, the first item is returned.
但是由于 sum
接受一个可迭代对象,你可以这样做:
sum([b for b in x if b > 0])
第二个代码片段中的输入是单个元素列表 - 这是一个退化的情况,甚至没有调用 callable。
考虑以下示例:
def my_sum(*args):
print "my_sum called with", args
return sum(*args)
x = [1, 2, 3]
reduce(my_sum, [b for b in x if b > 2])
# nothing gets printed, value 3 is returned, since it's only element in list
现在考虑你的失败案例:
reduce(my_sum, [b for b in x if b > 0])
输出为:
my_sum called with (1, 2)
[exception traceback]
对内置 sum 的等效调用是 sum(1, 2)
,它会导致与您引用的相同的异常。
sum
signature 不遵循 reduce 函数的规则。 sum(iterable[, start])
接受两个参数(正如预期的那样),但第一个必须是 可迭代的 ,其中第二个是可选的初始值。
reduce
要求函数接受两个参数,其中(引用文档):
The left argument, x, is the accumulated value and the right argument,
y, is the update value from the iterable.
我们可以清楚地看到这些接口彼此不一致。正确的可调用对象类似于:
def reduce_sum(accumulator, new_value):
return accumulator + new_value
reduce(reduce_sum, [b for b in x if b > 0])
正如摩西指出的那样"sum consumes the iterable in the first iteration"
让我们一起玩吧
>>> sum(1,2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> sum([1,2])
3
>>> sum(x)
6
>>> sum([b for b in x if b >0])
6
>>> reduce(lambda x, y: x+y, x)
6
>>> reduce(lambda x, y: x+y, [b for b in x if b > 0])
6
>>> reduce(lambda x, y: x+y, [b for b in x if b > 1])
5
我正在尝试学习 python reduce 函数。
这是一些对我来说没有意义的代码
>>> x = [1,2,3]
>>> reduce(sum, [b for b in x if b > 0])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> reduce(sum, [b for b in x if b > 2])
3
为什么当 b > 2 而不是 b > 0 时有效
代码看起来几乎一模一样
在第二种情况下,您使用列表理解创建的列表只有一个元素,因此不会减少。这种归约不会失败,因为那里没有任何东西可以归约,所以函数 sum 不会执行,也不会失败。结果是之前存在的唯一整数。
函数 sum 在 documentation 中定义。如您所见,它应该对某些可迭代容器(例如列表或元组)中的值求和。你可以写 sum([1,2,3])
,它会像你想要的减少一样工作。如果你想通过使用缩减函数来实现这个结果,你必须确保你有一个函数,它接受两个整数和 returns 它们的总和。
这里有这样的功能:
def sum2(x, y):
return x + y
用这样的函数减少应该会给你预期的结果。
sum2 = lambda x, y: x+y
下面的函数做同样的事情,但由于 lambda 表示法,它更短更好,如果您还不知道的话,将来某个时候看看它可能会很好。
来自docs 关于reduce
:
Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value
所以在第一种情况下,你错误地使用了 reduce
,因为你使用了内置的 sum
,它不接受两个参数,而是一个 iterable
。 sum
在 reduce
和 returns 和 int
的第一次迭代中消耗了可迭代对象,它不能进一步 reduceable.
在第二种情况下,sum
甚至没有在可迭代对象上调用:
If initializer is not given and iterable contains only one item, the first item is returned.
由于iterable属于length 1
,减少returns iterable中的一项,sum
并没有什么。
reduce
函数接收一个函数和一个可迭代对象。
然后它将可迭代对象的每个元素映射到函数。
您的第一个 b > 0
函数正在尝试使用 2 个参数 运行 sum
。一个参数是累计总数,另一个是输入列表的一个元素,例如它看起来像:sum(0, 1)
.
这在文档中说明为:
Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value. ... The left argument, is the accumulated value and the right argument, is the update value from the iterable.
您的第二个 b > 2
函数所做的只是 运行 使用包含一项的迭代器来简化 reduce 函数,它不会尝试应用 sum
,如文档中所述.
If initializer is not given and iterable contains only one item, the first item is returned.
但是由于 sum
接受一个可迭代对象,你可以这样做:
sum([b for b in x if b > 0])
第二个代码片段中的输入是单个元素列表 - 这是一个退化的情况,甚至没有调用 callable。
考虑以下示例:
def my_sum(*args):
print "my_sum called with", args
return sum(*args)
x = [1, 2, 3]
reduce(my_sum, [b for b in x if b > 2])
# nothing gets printed, value 3 is returned, since it's only element in list
现在考虑你的失败案例:
reduce(my_sum, [b for b in x if b > 0])
输出为:
my_sum called with (1, 2)
[exception traceback]
对内置 sum 的等效调用是 sum(1, 2)
,它会导致与您引用的相同的异常。
sum
signature 不遵循 reduce 函数的规则。 sum(iterable[, start])
接受两个参数(正如预期的那样),但第一个必须是 可迭代的 ,其中第二个是可选的初始值。
reduce
要求函数接受两个参数,其中(引用文档):
The left argument, x, is the accumulated value and the right argument, y, is the update value from the iterable.
我们可以清楚地看到这些接口彼此不一致。正确的可调用对象类似于:
def reduce_sum(accumulator, new_value):
return accumulator + new_value
reduce(reduce_sum, [b for b in x if b > 0])
正如摩西指出的那样"sum consumes the iterable in the first iteration" 让我们一起玩吧
>>> sum(1,2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> sum([1,2])
3
>>> sum(x)
6
>>> sum([b for b in x if b >0])
6
>>> reduce(lambda x, y: x+y, x)
6
>>> reduce(lambda x, y: x+y, [b for b in x if b > 0])
6
>>> reduce(lambda x, y: x+y, [b for b in x if b > 1])
5