Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于元组优先值的python求和元组列表_Python_List_Sum - Fatal编程技术网

基于元组优先值的python求和元组列表

基于元组优先值的python求和元组列表,python,list,sum,Python,List,Sum,假设我有以下列表元组: myList = [(0,2),(1,3),(2,4),(0,5),(1,6)] 我想根据相同的第一个元组值对该列表求和: [(n,m),(n,k),(m,l),(m,z)] = m*k + l*z 对于myList sum = 2*5 + 3*6 = 28 如何获得此信息?您可以使用集合。defaultdict: >>> from collections import defaultdict >>> from operator

假设我有以下列表元组:

myList = [(0,2),(1,3),(2,4),(0,5),(1,6)]
我想根据相同的第一个元组值对该列表求和:

[(n,m),(n,k),(m,l),(m,z)] = m*k + l*z
对于
myList

sum = 2*5 + 3*6 = 28

如何获得此信息?

您可以使用
集合。defaultdict

>>> from collections import defaultdict
>>> from operator import mul
>>> lis = [(0,2),(1,3),(2,4),(0,5),(1,6)]
>>> dic = defaultdict(list)
>>> for k,v in lis:
    dic[k].append(v)  #use the first item of the tuple as key and append second one to it
...     

#now multiply only those lists which contain more than 1 item and finally sum them.
>>> sum(reduce(mul,v) for k,v in dic.items() if len(v)>1)
 28

此解决方案只需一次,而可读性更强的
defaultdict
版本需要两次,并且可能占用更多空间:

myList = [(0,2),(1,3),(2,4),(0,5),(1,6)]
sum_ = 0
once, twice = {}, {}
for x, y in myList:
    if x in once:
        sum_ -= twice.get(x, 0)
        twice[x] = twice.get(x, once[x]) * y
        sum_ += twice[x]
    else:
        once[x] = y


>>> sum_
28

使用下面的程序,即使您有多个条目,而不是同一个密钥只有两个条目,它也会工作

#!/usr/local/bin/python3

myList = [(0,2),(1,3),(2,4),(0,5),(1,6),(1,2)]

h = {}
c = {}
sum = 0

for k in myList:
        # if key value already present
        if k[0] in c:
                if k[0] in h:
                        sum = sum - h[k[0]]
                        h[k[0]] = h[k[0]] * k[1]
                else:
                        h[k[0]] = c[k[0]] * k[1]
                sum = sum + h[k[0]]
        else:
                # stores key and value if first time though the loop
                c[k[0]] = k[1]                
print('sum is' + str(sum))

你的问题不是特别清楚输出应该如何计算……为什么(2,4)元素什么都没有发生?如果第一个元组值出现3次或更多次,您希望发生什么?对不起,我的英语很差。对于这些元组,如果它们有相同的第一个字段,则乘以它们的第二个字段。忽略没有相同第一个字段的元组。最后求多个值的和。@stephenlee不要担心你的英语很好,你的问题也很好,来自
groupby
docs:“一般来说,iterable需要在同一个键函数上排序”好了,现在我得到你的评论了。同意。与其说“此处无需排序”,不如说“此处无需排序和分组”:@JakubM。他的意思是有一种更有效的方法来做,汉克斯,这正是我需要的。
#!/usr/local/bin/python3

myList = [(0,2),(1,3),(2,4),(0,5),(1,6),(1,2)]

h = {}
c = {}
sum = 0

for k in myList:
        # if key value already present
        if k[0] in c:
                if k[0] in h:
                        sum = sum - h[k[0]]
                        h[k[0]] = h[k[0]] * k[1]
                else:
                        h[k[0]] = c[k[0]] * k[1]
                sum = sum + h[k[0]]
        else:
                # stores key and value if first time though the loop
                c[k[0]] = k[1]                
print('sum is' + str(sum))