如何将列表中的某些分数分配给多个列表中的值并获得 python 中每个值的总和?
How to assign certain scores from a list to values in multiple lists and get the sum for each value in python?
你能解释一下如何将一个列表中的某些分数分配给多个列表中的值,并得到每个值的总分吗?
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
的值,这会为每个字母分数组合提供一个 (key, value)
的元组。使每个压缩对象成为 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)
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)
与其将列表存储在单独的变量中,不如将它们放在列表的列表中,以便您可以遍历它并根据子列表中每个键的索引计算分数的总和:
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}
最简单的方法可能是使用 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))
打印结果,就像使用普通字典一样。
最后,小的 ells 和字母 O 作为变量名可能会很麻烦,因为它们很难与 ones 和 zeros 区分开来。 Python 风格指南(通常称为 PEP8)recommends avoiding them.
你可以 zip
两个 lists 和 score
作为一个 list l3
然后你可以使用 dictionary comprehension 和 filter
来构建你的 dicitonary。 key
是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}
{'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
你能解释一下如何将一个列表中的某些分数分配给多个列表中的值,并得到每个值的总分吗?
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
的值,这会为每个字母分数组合提供一个 (key, value)
的元组。使每个压缩对象成为 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)
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)
与其将列表存储在单独的变量中,不如将它们放在列表的列表中,以便您可以遍历它并根据子列表中每个键的索引计算分数的总和:
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}
最简单的方法可能是使用 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))
打印结果,就像使用普通字典一样。
最后,小的 ells 和字母 O 作为变量名可能会很麻烦,因为它们很难与 ones 和 zeros 区分开来。 Python 风格指南(通常称为 PEP8)recommends avoiding them.
你可以 zip
两个 lists 和 score
作为一个 list l3
然后你可以使用 dictionary comprehension 和 filter
来构建你的 dicitonary。 key
是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}
{'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