简体   繁体   English

根据另一个列表 python 计算列表中所有唯一元素的总和

[英]calculate sum of all unique elements in a list based on another list python

I have two lists like this,我有两个这样的清单,

a=[['a', 'b', 'c'], ['b', 'c'], ['a', 'd'], ['x']]
b=[[1, 2, 3], [4,5], [6,7], [8]] (the size of a and b is always same)

Now I want to create two list with the sum of unique elements, so the final lists should look like,现在我想用唯一元素的总和创建两个列表,所以最终列表应该看起来像,

 a=['a', 'b', 'c', 'd', 'x']
 b=[7, 6, 8, 7, 8] (sum of all a, b, d, d and x)

I could do this using for loop but looking for some efficient way to reduce execution time.我可以使用 for 循环来做到这一点,但要寻找一些有效的方法来减少执行时间。

Not so pythonic but will do the job:不是那么pythonic,但会完成这项工作:

a=[['a', 'b', 'c'], ['b', 'c'], ['a', 'd'], ['x']]
b=[[1, 2, 3], [4,5], [6,7], [8]]

mapn = dict()
for elt1, elt2 in zip(a, b):
    for e1, e2 in zip(elt1, elt2):
        mapn[e1] = mapn.get(e1, 0) + e2

elts = mapn.keys()
counts = mapn.values()

print(mapn)
print(elts)
print(counts)

You can use zip and collections.Counter along the following lines:您可以按照以下几行使用zipcollections.Counter

from collections import Counter

c = Counter()
for la, lb in zip(a, b):
    for xa, xb in zip(la, lb):
        c[xa] += xb
 
list(c.keys())
# ['a', 'b', 'c', 'd', 'x']
list(c.values())
# [7, 6, 8, 7, 8]

Here some ideas.这里有一些想法。

First, to flatten your list you can try:首先,要扁平化您的列表,您可以尝试:

a=[['a', 'b', 'c'], ['b', 'c'], ['a', 'd'], ['x']]
b=[[1, 2, 3], [4,5], [6,7], [8]]

To have uniques elements, you can do something like要拥有独特的元素,您可以执行以下操作

A = set([item for sublist in a for item in sublist])

But what I would do first (perhaps not the more efficient) is:但我首先要做的(也许不是更有效)是:

import pandas as pd
import bumpy as np
LIST1 = [item for sublist in a for item in sublist]
LIST2 = [item for sublist in b for item in sublist]
df = pd.DataFrame({'a':LIST1,'b':LIST2})
df.groupby(df.a).sum()

OUTPUT: OUTPUT:

在此处输入图像描述

At the end of the day, you're going to have to use two for loops.归根结底,您将不得不使用两个 for 循环。 I have a one liner solution using zip and Counter .我有一个使用zipCounter的单线解决方案。

The first solutions works only in this specific case where all the strings are a single character, because it creates a string with the right number of each letter, and then gets the frequency of each letter.第一个解决方案仅适用于所有字符串都是单个字符的特定情况,因为它创建一个每个字母的正确编号的字符串,然后获取每个字母的频率。

from collections import Counter

a = [['a', 'b', 'c'], ['b', 'c'], ['a', 'd'], ['x']]
b = [[1, 2, 3], [4,5], [6,7], [8]]

a, b = zip(*Counter(''.join(x*y for al, bl in zip(a, b) for x, y in zip(al, bl))).items())

For the more general case, you can do:对于更一般的情况,您可以执行以下操作:

a, b = zip(*Counter(dict(p for al, bl in zip(a, b) for p in zip(al, bl))).items())

You can combine the lists and their internal lists using zip(), then feed the list of tuples to a dictionary constructor to get a list of dictionaries with values for each letter.您可以使用 zip() 组合列表及其内部列表,然后将元组列表提供给字典构造函数,以获取包含每个字母值的字典列表。 Then convert those dictionaries to Counter and add them up.然后将这些字典转换为 Counter 并将它们相加。

a = [['a', 'b', 'c'], ['b', 'c'], ['a', 'd'], ['x']]
b = [[ 1,   2,   3 ], [ 4,   5 ], [ 6,   7 ], [ 8 ]]

from collections import Counter
from itertools import starmap

mapn        = sum(map(Counter,map(dict,starmap(zip,zip(a,b)))),Counter())
elts,counts = map(list,zip(*mapn.items()))

print(mapn)   # Counter({'c': 8, 'x': 8, 'a': 7, 'd': 7, 'b': 6})    
print(elts)   # ['a', 'b', 'c', 'd', 'x']    
print(counts) # [ 7,   6,   8,   7,   8]

detailed explanation:详细解释:

  • zip(a,b) combines the lists into pairs of sublists. zip(a,b)将列表组合成子列表对。 eg (['a','b','c'],[1,2,3]), ...例如 (['a','b','c'],[1,2,3]), ...
  • starmap(zip,...) takes these list pairs and merges then together into sublist of letter-number pairs: [('a',1),('b',2),('c',3)], ... starmap(zip,...)获取这些列表对,然后合并到字母数字对的子列表中:[('a',1),('b',2),('c',3)], ...
  • Each of these lists of pairs is converted to a dictionary by map(dict,...) and then into a Counter object by map(counter,...)这些对列表中的每一个都通过map(dict,...)转换为字典,然后通过map(counter,...)转换为 Counter object
  • We end up with a list of Counter objects corresponding to the pairing of each sublist.我们最终得到一个对应于每个子列表配对的 Counter 对象列表。 Applying sum(...,Counter()) computes the totals for each letter into a single Counter object.应用 sum(...,Counter()) 将每个字母的总数计算到单个计数器 object 中。
  • Apart from being a Counter object, mapn is excatly the same as the dictionary that you produced.除了作为计数器 object 之外,mapn 与您制作的字典完全一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基于python中另一个列表的索引的列表中特定元素的总和 - Sum of specific elements of a list based on indexes from another list in python 使用列表推导计算所有列表元素的总和 - Using list comprehension to calculate the sum of all list elements 结合基于另一个列表的Python列表元素 - Combine Python List Elements Based On Another List Python - 根据另一个列表的唯一元素从一个列表中随机采样一个元素 - Python - Sample one element randomly from a list based on the unique elements of another list 根据另一个列表中的序列迭代和求和第二个列表的元素 - Iterate and sum elements of second list based on sequences in another list 当一个列表的所有元素都在另一个列表中时,如何分组和求和 - How to group by and sum when all elements of one list are in another list 将列表的所有元素分别附加到 python 中另一个列表的所有元素 - Separately appending all the elements of a list to all the elements of another list in python python 列表中的元素总和 - Sum elements in python list Python循环可计算列表中除13之外的元素之和 - Python loop to calculate sum of elements in a list except 13 Python:根据另一个列表中的元素按索引从列表中删除元素 - Python: Removing elements from list by index based on elements in another list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM