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

时间:2018-10-03 02:48:14

标签: 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}

谢谢!

6 个答案:

答案 0 :(得分:3)

您可以将zip的值score添加到每个列表,这将为每个字母得分组合提供(key, value)的元组。将每个压缩的对象设为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}

答案 1 :(得分:0)

您最好使用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)

答案 2 :(得分:0)

from collections import defaultdict

score = [1,2,3,4,5] # note: 0 no need to use this list if there is no scenario like [5,6,9,10,4]
l_1 = ['a','b','c','d','e']
l_2 = ['c','a','d','e','b']

score_dict = defaultdict(int)

'''
for note: 0

if your score is always consecutive
like score = [2,3,4,5,6] or [5,6,7,8,9]...
you don't need to have seperate list of score you can set
start = score_of_char_at_first_position_ie_at_zero-th_index

like start = 2, or start = 5

else use this function

def add2ScoreDict( lst):
    for pos_score, char in zip(score,lst):
        score_dict[char] += pos_score

'''

def add2ScoreDict( lst):
    for pos, char in enumerate( lst,start =1):
        score_dict[char] += pos


# note: 1
add2ScoreDict( l_1)
add2ScoreDict( l_2)

#print(score_dict) # defaultdict(<class 'int'>, {'a': 3, 'b': 7, 'c': 4, 'd': 7, 'e': 9})

score_dict = dict(sorted(score_dict.items(), reverse = True, key=lambda x: x[1]))

print(score_dict) # {'e': 9, 'b': 7, 'd': 7, 'c': 4, 'a': 3} 

编辑1:

如果您有多个列表,请将它们放在list_of_list = [l_1, l_2]中,这样就不必一次又一次调用func add2ScoreDict。

# for note: 1
for lst in list_of_list:
    add2ScoreDict( lst)

答案 3 :(得分:0)

应该将列表存储在单独的变量中,而不是将列表存储在单独的变量中,以便可以对其进行迭代并根据子列表中每个键的索引计算分数总和:

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}

答案 4 :(得分:0)

最简单的方法可能是使用Python standard library中的Counter

from collections import Counter

tally = Counter()
scores = [1, 2, 3, 4, 5]


def add_scores(letters):
    for letter, score in zip(letters, scores):
        tally[letter] += score


L1 = ['a', 'b', 'c', 'd', 'e']
add_scores(L1)
L2 = ['c', 'a', 'd', 'e', 'b']
add_scores(L2)

print(tally)
>>> python tally.py
Counter({'e': 9, 'b': 7, 'd': 7, 'c': 4, 'a': 3})

zip用于配对字母和分数,一个for循环遍历它们,一个Counter收集结果。 Counter实际上是字典,因此您可以编写类似

的内容
tally['a']

获取字母a

的分数
for letter, score in tally.items():
    print('Letter %s scored %s' % (letter, score))

打印结果,就像使用普通字典一样。

最后,小写字母O和字母O可能会很麻烦,因为它们很难与1和0区分。 Python样式指南(通常称为PEP8)recommends avoiding them

答案 5 :(得分:0)

您可以f = @(x) sum(arrayfun(@(opp_ratings) (1/(1+10^((opp_ratings-x)/400))),opp_ratings))-tot_score 的两个列表,其中zip是一个列表 score,然后可以使用字典理解< / em>与l3一起构成您的字典filter是l3中新形成的 tuples 的索引key,其值是l3中创建后的所有索引1的总和。仅针对匹配索引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}

展开说明:

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