我有一个元组列表如下
[(1,4),(3,5),(2,9),(6,23),(3,21),(2,66),(5,20),(1,33),(3,55),(1,8)]
现在我需要这样的东西
如果有人可以帮助我,我会非常感激。我是python中的新手,所以很难找到它。
答案 0 :(得分:3)
这是一种方法。使用itertools.groupby
创建与每个索引对应的组,乘以并添加
from operator import itemgetter, mul
from itertools import groupby
z = [(1,4),(3,5),(2,9),(6,23),(3,21),(2,66),(5,20),(1,33),(3,55),(1,8)]
z = sorted(z, key=itemgetter(0))
z = groupby(z, key=itemgetter(0))
z = ((key, list(value for (key, value) in groups)) for (key, groups) in z)
z = ((key, reduce(mul, values, 1)) for (key, values) in z if len(values) > 1)
z = sum(value for (key, value) in z)
print z
7425
答案 1 :(得分:1)
将所有元组转换为dict,其中键是第一个元素,值是第二个元素的列表。过滤dict以获取具有多个元素的值。使用reduce()
将每个值的元素相乘,并sum()
将所有产品相加。
答案 2 :(得分:0)
另一种(略有不同)的方式。我们将元素收集到字典中,其中每个value
都是字典
import operator,collections
s=[(1,4),(3,5),(2,9),(6,23),(3,21),(2,66),(5,20),(1,33),(3,55),(1,8)]
s_d = collections.defaultdict(list)
for x in s:
s_d[x[0]].append(x[1])
s_d现在是
>>> s_d
defaultdict(<type 'list'>, {1: [4, 33, 8], 2: [9, 66], 3: [5, 21, 55], 5: [20], 6: [23]})
现在对于每个键,如果值字典的长度大于1,我们将这些值相乘,否则返回0
>>>map(lambda y: reduce(operator.mul,y[1],0) if len(y[1])>1 else 0,s_d.items())
[1056, 594, 5775, 0, 0]
但你想要总和,所以我们总结一下
>>>sum(map(lambda y: reduce(operator.mul,y[1],1) if len(y[1])>1 else 0,s_d.items()))
7425
答案 3 :(得分:0)
所以每个人都已经发布了内置函数的解决方案,但是编写一个循环并完成你需要的东西并不是多余的!
>>> l = [(1,4),(3,5),(2,9),(6,23),(3,21),(2,66),(5,20),(1,33),(3,55),(1,8)]
>>> hold = []
>>> for i in range(min(l)[0],max(l)[0] + 1):
... hold2 = []
... mult = 1
... for t in l:
... if t[0] == i:
... hold2.append(t[1])
... mult *= t[1]
... if len(hold2) > 1:
... hold.append(mult)
...
>>> sum(hold)
7425
你设置一个数组来检查每个索引的数量,然后迭代不同的索引并继续乘以匹配索引的下一个数字!
此外,这将是您获得的最快版本。仅仅因为它没有进口任何东西而且不需要花时间去做那件事。你给的每一个版本都不到一秒钟,但我很无聊所以我把它们计时:
Mine: 0:00:00.000477
1_CR: 00:00.073498
RedBaron: 0:00:00.079276
答案 4 :(得分:0)
上面的答案很棒,但不容易理解。不幸的是,在z
之后开始将groupby
中的一个打印为可读列表将使用1_CR指出的迭代器。这是解决方案,但中间步骤的打印输出。从逻辑上讲,每次打印后都需要重新开始从z3
开始的所有步骤。
from operator import itemgetter, mul
from itertools import groupby
from functools import reduce
import copy
z1 = [(1,4),(3,5),(2,9),(6,23),(3,21),(2,66),(5,20),(1,33),(3,55),(1,8)]
print('data: ', z1)
z2 = sorted(z1, key=itemgetter(0))
print('sorted: ', z2)
z3 = groupby(z2, key=itemgetter(0))
print('grouped:', [(x, list(y)) for x,y in z3])
z3 = groupby(z2, key=itemgetter(0))
z4 = ((key, list(value for (key, value) in groups)) for (key, groups) in z3)
print('lumped: ', list(z4))
z3 = groupby(z2, key=itemgetter(0))
z4 = ((key, list(value for (key, value) in groups)) for (key, groups) in z3)
z5 = ((key, reduce(mul, values, 1)) for (key, values) in z4 if len(values) > 1)
print('reduced:', list(z5))
z3 = groupby(z2, key=itemgetter(0))
z4 = ((key, list(value for (key, value) in groups)) for (key, groups) in z3)
z5 = ((key, reduce(mul, values, 1)) for (key, values) in z4 if len(values) > 1)
z6 = sum(value for (key, value) in z5)
print('sum: ', z6)
这是对这一切的奖励。我认为这有助于了解正在发生的事情。 如果您了解会发生什么,功能性编程可能很有趣。
data: [(1, 4), (3, 5), (2, 9), (6, 23), (3, 21), (2, 66), (5, 20), (1, 33), (3, 55), (1, 8)]
sorted: [(1, 4), (1, 33), (1, 8), (2, 9), (2, 66), (3, 5), (3, 21), (3, 55), (5, 20), (6, 23)]
grouped: [(1, [(1, 4), (1, 33), (1, 8)]), (2, [(2, 9), (2, 66)]), (3, [(3, 5), (3, 21), (3, 55)]), (5, [(5, 20)]), (6, [(6, 23)])]
lumped: [(1, [4, 33, 8]), (2, [9, 66]), (3, [5, 21, 55]), (5, [20]), (6, [23])]
reduced: [(1, 1056), (2, 594), (3, 5775)]
sum: 7425
答案 5 :(得分:0)
这是一个简单的程序版本:
tups=[(1,4),(3,5),(2,9),(6,23),(3,21),(2,66),(5,20),(1,33),(3,55),(1,8)]
di={}
for t in tups:
di.setdefault(t[0],[]).append(t[1])
ans=0
for k in di:
if len(di[k])==1: continue
x=1
for e in di[k]: x*=e
ans+=x
print ans
打印7425
如果您想要简短程序版本:
di={}
for t in tups:
di.setdefault(t[0],[]).append(t[1])
print sum(reduce(lambda x,y: x*y,l) for l in di.values() if len(l)>1)
这是一个棘手的版本(来自狼):
di={}
{di.setdefault(t[0],[]).append(t[1]) for t in tups}
print sum(reduce(lambda x,y: x*y,l) for l in di.values() if len(l)>1)
答案 6 :(得分:0)
以下是使用词典的答案:
def calculate(t):
t1 = {} # first time we encounter an index
t2 = {} # product of values
for index, value in t:
if index in t1: # we have seen this index already
if index not in t2: # start the multiplier
t2[index] = t1[index]
t2[index] *= value # chain multiplication
else:
t1[index] = value # first time for this index
return sum(t2.values()) # sum of multipliers