我有两个列表作为全局列表,一个作为函数中的本地列表
List1 = [[['MP01'], 0], [['MP02'], 0], [['MP03'], 0], [['MP04'], 0], [['MP05'], 0], [['MP06'], 0], [['MP07'], 0], [['MP08'], 0], [['MP09'], 0], [['MP10'], 0], [['MP11'], 0], [['MP12'], 0], [['MP13'], 0], [['MP14'], 0], [['MP15'], 0], [['MP16'], 0], [['MP17'], 0], [['MP18'], 0], [['MP19'], 0], [['MP20'], 0], [['MP21'], 0], [['MP22'], 0], [['MP23'], 0], [['MP24'], 0], [['MP25'], 0], [['MP26'], 0], [['MP27'], 0], [['MP28'], 0], [['MP29'], 0], [['MP30'], 0], [['MP31'], 0], [['MP32'], 0]]
function():
List2 = [['MP18', 100], ['MP28', 50], ['MP20', 30], ['MP07', 30], ['MP12', 10], ['MP03', 10], ['MP30', 10], ['MP14', 10], ['MP15', 5], ['MP16', 5], ['MP13', 5], ['MP02', 5], ['MP26', 5], ['MP23', 5], ['MP22 ', 5], ['MP01', 5]]
final_list = [[[a], b+dict(List2).get(a, 0)] for [a], b in List1]
基本上," List2"表示玩家在一轮游戏中获得的分数(MP18得到100,MP28得到50等)和#34; List1"将用于为每位玩家累积多轮游戏的分数。
我想要做的事实上是将每个球员得分加到" List1"我遇到的问题是,因为" final_list"在函数内部,每次调用函数并为" List2"产生新值,将新值添加到" List1"它只是覆盖它们。例如,在第一轮之后" List1"将具有来自" List2"
的玩家的更新值['MP18', 100], ['MP28', 50] etc
因此MP18将获得100分,MP28将获得50分等等。但是,在第二轮计划中(第二轮)" List2"将为每个玩家提供不同的值,例如:
[['MP18, 50], [MP28, 100] etc
但是因为" final_list"正在从函数内部创建,第一组分数被第二组覆盖而不是第二组被添加到第一组如果有意义的话?所以在第二次运行" List1"看起来像是:
[[MP28, 100], [MP18, 50]
反对所期望的结果:
[[MP28, 150], [MP18, 150]]
希望这是一个有用的解释:D
答案 0 :(得分:1)
你可以试试这个:
List1 = [[['MP01'], 0], [['MP02'], 0], [['MP03'], 0], [['MP04'], 0], [['MP05'], 0], [['MP06'], 0], [['MP07'], 0], [['MP08'], 0], [['MP09'], 0], [['MP10'], 0], [['MP11'], 0], [['MP12'], 0], [['MP13'], 0], [['MP14'], 0], [['MP15'], 0], [['MP16'], 0], [['MP17'], 0], [['MP18'], 0], [['MP19'], 0], [['MP20'], 0], [['MP21'], 0], [['MP22'], 0], [['MP23'], 0], [['MP24'], 0], [['MP25'], 0], [['MP26'], 0], [['MP27'], 0], [['MP28'], 0], [['MP29'], 0], [['MP30'], 0], [['MP31'], 0], [['MP32'], 0]]
List2 = [['MP18', 100], ['MP28', 50], ['MP20', 30], ['MP07', 30], ['MP12', 10], ['MP03', 10], ['MP30', 10], ['MP14', 10], ['MP15', 5], ['MP16', 5], ['MP13', 5], ['MP02', 5], ['MP26', 5], ['MP23', 5], ['MP22 ', 5], ['MP01', 5]]
final_list = [[[a], b+dict(List2).get(a, 0)] for [a], b in List1]
然后,稍后,将列表中的值相乘:
new_final_list = [[[a], 2*b] for [a], b in List1]
输出:
[[['MP01'], 0], [['MP02'], 0], [['MP03'], 0], [['MP04'], 0], [['MP05'], 0], [['MP06'], 0], [['MP07'], 0], [['MP08'], 0], [['MP09'], 0], [['MP10'], 0], [['MP11'], 0], [['MP12'], 0], [['MP13'], 0], [['MP14'], 0], [['MP15'], 0], [['MP16'], 0], [['MP17'], 0], [['MP18'], 0], [['MP19'], 0], [['MP20'], 0], [['MP21'], 0], [['MP22'], 0], [['MP23'], 0], [['MP24'], 0], [['MP25'], 0], [['MP26'], 0], [['MP27'], 0], [['MP28'], 0], [['MP29'], 0], [['MP30'], 0], [['MP31'], 0], [['MP32'], 0]]
编辑:
关于您最近的评论,您可以尝试在该功能之外创建final_list
:
List1 = [[['MP01'], 0], [['MP02'], 0], [['MP03'], 0], [['MP04'], 0], [['MP05'], 0], [['MP06'], 0], [['MP07'], 0], [['MP08'], 0], [['MP09'], 0], [['MP10'], 0], [['MP11'], 0], [['MP12'], 0], [['MP13'], 0], [['MP14'], 0], [['MP15'], 0], [['MP16'], 0], [['MP17'], 0], [['MP18'], 0], [['MP19'], 0], [['MP20'], 0], [['MP21'], 0], [['MP22'], 0], [['MP23'], 0], [['MP24'], 0], [['MP25'], 0], [['MP26'], 0], [['MP27'], 0], [['MP28'], 0], [['MP29'], 0], [['MP30'], 0], [['MP31'], 0], [['MP32'], 0]]
def some_function():
List2 = [['MP18', 100], ['MP28', 50], ['MP20', 30], ['MP07', 30], ['MP12', 10], ['MP03', 10], ['MP30', 10], ['MP14', 10], ['MP15', 5], ['MP16', 5], ['MP13', 5], ['MP02', 5], ['MP26', 5], ['MP23', 5], ['MP22 ', 5], ['MP01', 5]]
global List1
List1 = [[[a], b+dict(List2).get(a, 0)] for [a], b in List1]