Python 中 filter() 之后的 Reduce()?
Reduce() after filter() in Python?
我对 python 中的两个函数有疑问:reduce() 和 filter()。
我们可以在 filter() 之后使用 reduce() 吗?
我在 sklearn 中使用了波士顿数据集。
x = load_boston()
x_target = x.target
xx = filter(lambda x: x > 20, x_target)
它工作正常。
接下来我想使用 reduce() 函数对 xx.
中的值求和
from functools import reduce
xxx = reduce(lambda x,y: x+y, xx)
我收到错误:
TypeError Traceback (most recent call last)
<ipython-input-64-062fcc861672> in <module>()
1 from functools import reduce
----> 2 xxx = reduce(lambda x,y: x+y, xx)
TypeError: reduce() of empty sequence with no initial value
有什么建议吗?
这意味着过滤器函数 returns 是您列表中的一个空列表。这里有一个例子:
sample = [2,3,4,5,6,7,8]
filter(lambda x: x%2 == 0, sample)
>>> [2, 4, 6, 8]
reduce(lambda x,y: x+y, filter(lambda x: x%2 == 0, sample))
>>> 20
那么,您的代码应该可以工作。
这是python 2.7。 python 3+
应该不同
编辑:python3
from functools import reduce
sample = [2,3,4,5,6,7,8]
f = filter(lambda x: x%2 == 0, sample)
reduce(lambda x,y: x+y, f)
>>> 20
以同样的方式工作; )
是的,您可以在 reduce()
中使用 filter()
对象就好了:
>>> from functools import reduce
>>> values = range(10, 30)
>>> filtered = filter(lambda x: x > 20, values)
>>> reduce(lambda x, y: x + y, filtered)
225
然而,一个filter()
对象是一个迭代器;它会根据需要产生过滤后的值,当它到达终点时不会产生任何其他东西。所以你需要确保在将它传递给 reduce()
:
之前你没有清空它
>>> filtered = filter(lambda x: x > 20, values)
>>> filtered
<filter object at 0x10ee64ac8>
>>> list(filtered)
[21, 22, 23, 24, 25, 26, 27, 28, 29]
>>> reduce(lambda x, y: x + y, filtered)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: reduce() of empty sequence with no initial value
当您需要在多个地方重新使用它时,重新创建 filter()
对象。
我对 python 中的两个函数有疑问:reduce() 和 filter()。 我们可以在 filter() 之后使用 reduce() 吗?
我在 sklearn 中使用了波士顿数据集。
x = load_boston()
x_target = x.target
xx = filter(lambda x: x > 20, x_target)
它工作正常。 接下来我想使用 reduce() 函数对 xx.
中的值求和from functools import reduce
xxx = reduce(lambda x,y: x+y, xx)
我收到错误:
TypeError Traceback (most recent call last)
<ipython-input-64-062fcc861672> in <module>()
1 from functools import reduce
----> 2 xxx = reduce(lambda x,y: x+y, xx)
TypeError: reduce() of empty sequence with no initial value
有什么建议吗?
这意味着过滤器函数 returns 是您列表中的一个空列表。这里有一个例子:
sample = [2,3,4,5,6,7,8]
filter(lambda x: x%2 == 0, sample)
>>> [2, 4, 6, 8]
reduce(lambda x,y: x+y, filter(lambda x: x%2 == 0, sample))
>>> 20
那么,您的代码应该可以工作。
这是python 2.7。 python 3+
应该不同编辑:python3
from functools import reduce
sample = [2,3,4,5,6,7,8]
f = filter(lambda x: x%2 == 0, sample)
reduce(lambda x,y: x+y, f)
>>> 20
以同样的方式工作; )
是的,您可以在 reduce()
中使用 filter()
对象就好了:
>>> from functools import reduce
>>> values = range(10, 30)
>>> filtered = filter(lambda x: x > 20, values)
>>> reduce(lambda x, y: x + y, filtered)
225
然而,一个filter()
对象是一个迭代器;它会根据需要产生过滤后的值,当它到达终点时不会产生任何其他东西。所以你需要确保在将它传递给 reduce()
:
>>> filtered = filter(lambda x: x > 20, values)
>>> filtered
<filter object at 0x10ee64ac8>
>>> list(filtered)
[21, 22, 23, 24, 25, 26, 27, 28, 29]
>>> reduce(lambda x, y: x + y, filtered)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: reduce() of empty sequence with no initial value
当您需要在多个地方重新使用它时,重新创建 filter()
对象。