你如何减少Python 3中的元组列表?

时间:2018-04-20 20:04:56

标签: python list tuples reduce

问题

输入:

input_list = [(1, 2), (1, 4), (1, 6)]

预期产出:

(3, 12)

我试过

print(reduce(lambda a, b: (a[0] + b[0], a[1] + b[1]), input_list))

print(reduce(lambda (a, b), (c, d): (a + c, b + d), input_list))

由于语法无效而失败。

1 个答案:

答案 0 :(得分:0)

在Python 3中,reduce已从builtins移至functools,因此您需要from functools import reduce

相关问题