Python 按元素添加两个元组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16548584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:58:04  来源:igfitidea点击:

Adding two tuples elementwise

pythontuples

提问by James

I was just wondering if there was an especially pythonic way of adding two tuples elementwise?

我只是想知道是否有一种特别的 Pythonic 方式来元素添加两个元组?

So far (a and b are tuples), I have

到目前为止(a 和 b 是元组),我有

map(sum, zip(a, b))

My expected output would be:

我的预期输出是:

(a[0] + b[0], a[1] + b[1], ...)

And a possible weighing would be to give a 0.5 weight and b 0.5 weight, or so on. (I'm trying to take a weighted average).

一个可能的称重是给出一个 0.5 的重量和一个 0.5 的重量,等等。(我正在尝试取加权平均值)。

Which works fine, but say I wanted to add a weighting, I'm not quite sure how I would do that.

哪个工作正常,但说我想添加一个权重,我不太确定我会怎么做。

Thanks

谢谢

采纳答案by Chris Doggett

Zip them, then sum each tuple.

压缩它们,然后对每个元组求和。

[sum(x) for x in zip(a,b)]

EDIT :Here's a better, albeit more complex version that allows for weighting.

编辑:这是一个更好的,尽管更复杂的版本,它允许加权。

from itertools import starmap, islice, izip

a = [1, 2, 3]
b = [3, 4, 5]
w = [0.5, 1.5] # weights => a*0.5 + b*1.5

products = [m for m in starmap(lambda i,j:i*j, [y for x in zip(a,b) for y in zip(x,w)])]

sums = [sum(x) for x in izip(*[islice(products, i, None, 2) for i in range(2)])]

print sums # should be [5.0, 7.0, 9.0]

回答by poke

>>> a = (1, 2, 3)
>>> b = (4, 5, 6)
>>> def averageWeightedSum(args):
        return sum(args) / len(args)
>>> tuple(map(averageWeightedSum, zip(a, b)))
(2.5, 3.5, 4.5)

An alternative would be to apply the weights first. This would also allow you to have different weights:

另一种方法是先应用权重。这也将允许您拥有不同的权重:

>>> from operator import mul
>>> weights = (0.3, 0.7)
>>> tuple(sum(map(mul, x, weights)) for x in zip(a, b))
(3.0999999999999996, 4.1, 5.1)
>>> weights = (0.5, 0.5)
>>> tuple(sum(map(mul, x, weights)) for x in zip(a, b))
(2.5, 3.5, 4.5)

回答by alexis

Take the formula for the weighted sum of one pair of coordinates, and form a tuple with an iterator over each pair (note the two variables after the for):

取一对坐标的加权和的公式,并在每对坐标上形成一个带有迭代器的元组(注意 后面的两个变量for):

tuple(0.5*an + 0.5*bn for an, bn in zip(a, b))

This keeps it simple and readable as a one-liner. Of course if your "weighted sum" is a complicated function, you'd define it as a separate function first.

这使它作为单行代码保持简单和可读。当然,如果您的“加权总和”是一个复杂的函数,您应该首先将其定义为一个单独的函数。

回答by dzhelil

If you do not mind the dependency, you can use numpy for elementwise operations on arrays

如果你不介意依赖,你可以使用 numpy 对数组进行元素操作

>>> import numpy as np
>>> a = np.array([1, 2, 3])
>>> b = np.array([3, 4, 5])
>>> a + b
array([4, 6, 8])

回答by jeromej

If your tuples contain strobjects:

如果您的元组包含str对象:

list(map(''.join, zip('abc', '123')))
# Returns ['a1', 'b2', 'c3']