Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_Dictionary_Cumulative Sum - Fatal编程技术网

如何将列表中的某些分数分配给多个列表中的值,并在python中获得每个值的总和?

如何将列表中的某些分数分配给多个列表中的值,并在python中获得每个值的总和?,python,list,dictionary,cumulative-sum,Python,List,Dictionary,Cumulative Sum,您能解释一下如何将列表中的某些分数分配给多个列表中的值,并获得每个值的总分吗 score=[1,2,3,4,5]根据列表中的位置分配分数 l_1=[a,b,c,d,e] 分配a=1,b=2,c=3,d=4,e=5 l_2=[c,a,d,e,b] 分配c=1,a=2,d=3,e=4,b=5 我试图得到这样的结果 {'e':9,'b':7,'d':7,'c':4,'a':3} 谢谢大家! 您可以zip将score的值添加到每个列表中,从而为每个字母分数组合提供(键,值)的元组。使每个压缩对象成为一个

您能解释一下如何将列表中的某些分数分配给多个列表中的值,并获得每个值的总分吗

score=[1,2,3,4,5]
根据列表中的位置分配分数

l_1=[a,b,c,d,e]
分配a=1,b=2,c=3,d=4,e=5

l_2=[c,a,d,e,b]
分配c=1,a=2,d=3,e=4,b=5

我试图得到这样的结果
{'e':9,'b':7,'d':7,'c':4,'a':3}


谢谢大家!

您可以
zip
score
的值添加到每个列表中,从而为每个字母分数组合提供
(键,值)
的元组。使每个压缩对象成为一个
dict
。然后使用dict理解将每个键的值相加

d_1 = dict(zip(l_1, score))
d_2 = dict(zip(l_2, score))

{k: v + d_2[k] for k, v in d_1.items()}
# {'a': 3, 'b': 7, 'c': 4, 'd': 7, 'e': 9}

您最好使用
zip
功能:

dic = {'a':0, 'b': 0, 'c':0, 'd': 0, 'e': 0}
def score(dic, *args):
    for lst in args:
        for k, v in zip(lst, range(len(lst))):
            dic[k] += v+1
    return dic

l_1 = ['a','b','c','d','e']
l_2 = ['c','a','d','e','b']

score(dic, l_1, l_2)

不应将列表存储在单独的变量中,而应将它们放在列表列表中,这样您就可以遍历列表,并根据子列表中每个键的索引计算分数总和:

score = [1, 2, 3, 4, 5]
lists = [
    ['a','b','c','d','e'],
    ['c','a','d','e','b']
]
d = {}
for l in lists:
    for i, k in enumerate(l):
        d[k] = d.get(k, 0) + score[i]
d
将变成:

{'a': 3, 'b': 7, 'c': 4, 'd': 7, 'e': 9}

最简单的方法可能是使用来自的
计数器

zip
用于字母和分数配对,一个
for
循环用于迭代,一个
计数器用于收集结果。
计数器实际上是一本字典,因此您可以编写如下内容

tally['a']
获取字母
a
的分数,或

for letter, score in tally.items():
    print('Letter %s scored %s' % (letter, score))
打印结果,就像使用普通词典一样


最后,小ELL和字母O作为变量名可能会很麻烦,因为它们很难区分1和0。Python风格指南(通常称为PEP8)。

您可以将
zip
两个带有
score
的列表作为一个列表
l3
,然后您可以使用字典理解和
filter
构建您的词典。
是l3中新形成元组的索引
1
,该值是创建子列表后l3中所有索引
0
的总和,该子列表仅针对匹配的索引
0

score = [1,2,3,4,5] 

l_1 = ['a', 'b', 'c', 'd', 'e'] 
l_2 = ['c', 'a', 'd', 'e', 'b'] 
l3 = [*zip(score, l_1), *zip(score,l_2)]

d = {i[1]: sum([j[0] for j in list(filter(lambda x: x[1] ==i[1], l3))]) for i in l3}
扩展说明:

d = {}
for i in l3:
    f = list(filter(lambda x: x[1] == i[1], l3))
    vals = []
    for j in f:
        vals.append(j[0])
    total_vals = sum(vals)
    d[i[1]] = total_vals

你试了什么?
for letter, score in tally.items():
    print('Letter %s scored %s' % (letter, score))
score = [1,2,3,4,5] 

l_1 = ['a', 'b', 'c', 'd', 'e'] 
l_2 = ['c', 'a', 'd', 'e', 'b'] 
l3 = [*zip(score, l_1), *zip(score,l_2)]

d = {i[1]: sum([j[0] for j in list(filter(lambda x: x[1] ==i[1], l3))]) for i in l3}
{'a': 3, 'b': 7, 'c': 4, 'd': 7, 'e': 9}
d = {}
for i in l3:
    f = list(filter(lambda x: x[1] == i[1], l3))
    vals = []
    for j in f:
        vals.append(j[0])
    total_vals = sum(vals)
    d[i[1]] = total_vals