I am able to use map
and sum
to achieve this functionality, but how to use reduce
?
我可以使用map和sum来实现这个功能,但是如何使用reduce呢?
There are 2 lists: a
, b
, they have same number of values. I want to calculate
有两个列表:a, b,它们有相同数量的值。我要计算
a[0]*b[0]+a[1]*b[1]+...+a[n]*b[n]
The working version I wrote using map
is
我使用map编写的工作版本是。
value = sum(map(lambda (x,y): x*y, zip(a, b)))
How to use reduce
then? I wrote:
那么如何减少使用呢?我写:
value = reduce(lambda (x,y): x[0]*y[0] + x[1]*y[1], zip(a, b)))
I got the error "TypeError: 'float' object is unsubscriptable
".
我得到了错误的“类型错误:‘float’对象是不可订阅的”。
Can anyone shed some light on this?
有人能解释一下吗?
5 个解决方案
#1
9
The first argument of the lambda function is the sum so far and the second argument is the next pair of elements:
lambda函数的第一个参数是迄今为止的和,第二个参数是下一对元素:
value = reduce(lambda sum, (x, y): sum + x*y, zip(a, b), 0)
#2
7
I would do it this way (I don't think you need lambda)...
我这样做(我认为你不需要)……
sum(x*y for x, y in zip(a, b))
This also seems slightly more explicit. Zip AB, multiply them, and sum up the terms.
这似乎也更加明确。压缩AB,乘以它们,然后求和。
#3
7
A solution using reduce
and map
,
使用reduce和map的解决方案,
from operator import add,mul
a = [1,2,3]
b = [4,5,6]
print reduce(add,map(mul,a,b))
#4
1
Difficulties with reduce happen when you have incorrect map.
当你有不正确的地图时,会有减少的困难。
Let's take expression: value = sum(map(lambda (x,y): x*y, zip(a, b)))
我们取表达式:value = sum(映射(x,y): x*y, zip(a, b))
Map is transformation. We need it to convert tuples into simple flat values. In your case it will look like:
映射转换。我们需要它将元组转换成简单的平值。在你的情况下,它会是:
map(lambda x: x[0]*x[1], zip(a,b))
And then, if you want to express sum
via reduce
- it will look like:
然后,如果你想通过reduce来表示求和,它会是:
reduce(lambda x,y: x + y, map)
So, here is example:
这是例子:
a = [1,2,3]
b = [4,5,6]
l = zip(a,b)
m = map(lambda x: x[0]*x[1], l)
r = reduce(lambda x,y: x + y, m)
#5
0
it looks like you want an inner product. use an inner product. https://docs.scipy.org/doc/numpy/reference/generated/numpy.inner.html
看起来你想要一个内积。使用一个内积。https://docs.scipy.org/doc/numpy/reference/generated/numpy.inner.html
np.inner(a, b) = sum(a[:]*b[:])
Ordinary inner product for vectors:
向量的普通内积:
a = np.array([1,2,3])
b = np.array([0,1,0])
np.inner(a, b)
output: 2
输出:2
A multidimensional example:
一个多维的例子:
a = np.arange(24).reshape((2,3,4))
b = np.arange(4)
np.inner(a, b)
output: array([[ 14, 38, 62],[ 86, 110, 134]])
输出:数组([[14,38,62],[86,110,134])
#1
9
The first argument of the lambda function is the sum so far and the second argument is the next pair of elements:
lambda函数的第一个参数是迄今为止的和,第二个参数是下一对元素:
value = reduce(lambda sum, (x, y): sum + x*y, zip(a, b), 0)
#2
7
I would do it this way (I don't think you need lambda)...
我这样做(我认为你不需要)……
sum(x*y for x, y in zip(a, b))
This also seems slightly more explicit. Zip AB, multiply them, and sum up the terms.
这似乎也更加明确。压缩AB,乘以它们,然后求和。
#3
7
A solution using reduce
and map
,
使用reduce和map的解决方案,
from operator import add,mul
a = [1,2,3]
b = [4,5,6]
print reduce(add,map(mul,a,b))
#4
1
Difficulties with reduce happen when you have incorrect map.
当你有不正确的地图时,会有减少的困难。
Let's take expression: value = sum(map(lambda (x,y): x*y, zip(a, b)))
我们取表达式:value = sum(映射(x,y): x*y, zip(a, b))
Map is transformation. We need it to convert tuples into simple flat values. In your case it will look like:
映射转换。我们需要它将元组转换成简单的平值。在你的情况下,它会是:
map(lambda x: x[0]*x[1], zip(a,b))
And then, if you want to express sum
via reduce
- it will look like:
然后,如果你想通过reduce来表示求和,它会是:
reduce(lambda x,y: x + y, map)
So, here is example:
这是例子:
a = [1,2,3]
b = [4,5,6]
l = zip(a,b)
m = map(lambda x: x[0]*x[1], l)
r = reduce(lambda x,y: x + y, m)
#5
0
it looks like you want an inner product. use an inner product. https://docs.scipy.org/doc/numpy/reference/generated/numpy.inner.html
看起来你想要一个内积。使用一个内积。https://docs.scipy.org/doc/numpy/reference/generated/numpy.inner.html
np.inner(a, b) = sum(a[:]*b[:])
Ordinary inner product for vectors:
向量的普通内积:
a = np.array([1,2,3])
b = np.array([0,1,0])
np.inner(a, b)
output: 2
输出:2
A multidimensional example:
一个多维的例子:
a = np.arange(24).reshape((2,3,4))
b = np.arange(4)
np.inner(a, b)
output: array([[ 14, 38, 62],[ 86, 110, 134]])
输出:数组([[14,38,62],[86,110,134])