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 - Fatal编程技术网

Python基于另一个列表更新全局列表

Python基于另一个列表更新全局列表,python,list,Python,List,我正在努力重新编写一些代码,这些代码必须执行稍微不同的功能。基本上我有一个清单: totalRank = [['P1'], 0], [['P2'], 0], [['P3'], 0], [['P4'], 0], [['P5']] 我还有第二个类似的清单: playerPoints = [['P1', 5], ['P2', 5], ['P3', 0]. ['P4', 0]] 我想做的是根据“玩家点数”列表中的点数更新“totalRank”列表中的玩家,期望的结果是: totalRank = [[

我正在努力重新编写一些代码,这些代码必须执行稍微不同的功能。基本上我有一个清单:

totalRank = [['P1'], 0], [['P2'], 0], [['P3'], 0], [['P4'], 0], [['P5']]
我还有第二个类似的清单:

playerPoints = [['P1', 5], ['P2', 5], ['P3', 0]. ['P4', 0]]
我想做的是根据“玩家点数”列表中的点数更新“totalRank”列表中的玩家,期望的结果是:

totalRank = [['P1'], 5], [['P2'], 5], [['P3'], 0], [['P4'], 0], [['P5', 0]]
以下是我必须执行此操作的代码:

global totalRank
totalRank = [[[a], b+dict(playerPoints).get(a, 0)] for [a], b in totalRank]
此代码确实有效。但是,理想的效果不是将“玩家点数”列表中的点数添加到“totalRank”列表中,而是更新它们或“使它们相等”(如果有意义)?例如,下一个“玩家点数”列表可能是:

playerPoints = [['P1', 20], ['P2', 20], ['P3', 10]. ['P4', 0]]
“totalRank”列表的预期结果是:

totalRank = [['P1'], 20], [['P2'], 20], [['P3'], 10], [['P4'], 0], [['P5'], 0]]
鉴于目前我的代码将生成以下列表:

totalRank = [['P1'], 25], [['P2'], 25], [['P3'], 10], [['P4'], 0], [['P5'], 0]]
在这方面的任何帮助都将不胜感激,因为我已经挣扎了很长一段时间了

您可以尝试以下方法:

totalRank = [[['P1'], 0], [['P2'], 0], [['P3'], 0], [['P4'], 0], [['P5']]]
playerPoints = [['P1', 5], ['P2', 5], ['P3', 0], ['P4', 0]]
final_d = dict(playerPoints)
new_data = [[[a], b[0]+final_d.get(a, 0) if b else final_d.get(a, 0)] for [a], *b in totalRank] 
输出:

[[['P1'], 5], [['P2'], 5], [['P3'], 0], [['P4'], 0], [['P5'], 0]]
[[['P1'], 25], [['P2'], 25], [['P3'], 10], [['P4'], 0], [['P5'], 0]]
编辑:解决方案不适用于其他输入:

totalRank = [[['P1'], 5], [['P2'], 5], [['P3'], 0], [['P4'], 0], [['P5'], 0]]
playerPoints = [['P1', 20], ['P2', 20], ['P3', 10], ['P4', 0]]
final_d = dict(playerPoints)
new_data = [[[a], b[0]+final_d.get(a, 0) if b else final_d.get(a, 0)] for [a], *b in totalRank] 
输出:

[[['P1'], 5], [['P2'], 5], [['P3'], 0], [['P4'], 0], [['P5'], 0]]
[[['P1'], 25], [['P2'], 25], [['P3'], 10], [['P4'], 0], [['P5'], 0]]
这应该起作用:

totalRank = {'P1': 0, 'P2': 0, 'P3':0, 'P4': 0, 'P5':0}
playerPoints = {'P1':5,'P2':5,'P3':0,'P4':0}

for key in playerPoints.keys():
    totalRank[key] = playerPoints[key]

print(totalRank)
这将有以下输出:

[[['P1'], 5], [['P2'], 5], [['P3'], 0], [['P4'], 0], [['P5'], 0]]

字典的威力在于,此代码适用于您在playerPoints中输入的任何键。字典的格式是
{key1:value1、key2:value2、key3:value3}
等等。添加玩家点数时请记住这一点。

试试这个。它适用于我与您指定的playerLists值

def updateList(totalRank, playerPoints):

    for plist in playerPoints:
        pkey = plist[0]
        pvalue = plist[1]

        for j in range(len(totalRank)):
            tkey = totalRank[j][0]
            tvalue = totalRank[j][1]
            if pkey in tkey:
                totalRank[j][0] = pkey
                totalRank[j][1] = pvalue

    return totalRank

### Input starts here

totalRank = [[['P1'], 0], [['P2'], 0], [['P3'], 0], [['P4'], 0], [['P5']]]
playerPoints = [['P1', 5], ['P2', 5], ['P3', 0], ['P4', 0]]

### take care of key without explicit value, e.g. 'P5' with no value
for list in totalRank:
    if len(list) == 1:
        list.append(0)

print ("totalRank = ", totalRank)

totalRank = updateList(totalRank, playerPoints)
print ("totalRank = ", totalRank)

playerPoints = [['P1', 20], ['P2', 20], ['P3', 10], ['P4', 0]]
totalRank = updateList(totalRank, playerPoints)
print ("totalRank = ", totalRank)
使用您的输入从上述运行中获得的示例输出:

totalRank =  [[['P1'], 0], [['P2'], 0], [['P3'], 0], [['P4'], 0], [['P5'], 0]]
totalRank =  [['P1', 5], ['P2', 5], ['P3', 0], ['P4', 0], [['P5'], 0]]
totalRank =  [['P1', 20], ['P2', 20], ['P3', 10], ['P4', 0], [['P5'], 0]]

注意[['P1',x],…已更改为[['P1',x]但是,这些值是有效的。

从你描述的场景来看,我认为两个字典可能比两个列表更合适。如果可行的话,也许你应该考虑在这个项目中把列表改成字典?嘿,谢谢你的回复!不幸的是,“PoalPosits”列表被操纵和创造了。通过我的项目,“totalRank”列表基本上作为一个列表,它将记录每个玩家的分数,并在每次创建新的“playerPoints”列表时进行更新。看起来你的括号已为底部的两个列表关闭,现在正在处理一个答案。我的答案应该可以解决问题,而无需词典。此外,我不明白为什么您对捷豹的回复意味着您不能将两者都更改为dict。一个是通过您的项目操纵和创建的,您可以使用dict像列表一样轻松地完成。另一个是为每个玩家保留分数日志,并在每次创建新的
playerPoints
时更新,该点数也可以是dict,并且在概念上更简单这样做(并使此代码和其他代码更易于编写,效率更高)。嘿,谢谢你的回复!我测试了这个,但是它给出的结果与我以前的代码相同。因此,当下一个“playerPoints”列表通过时,它会添加结果,而不是将其更新为新结果。例如,如果通过的下一个“playerPoints”列表的值为10,则“totalRank”或“new_data”列表的值为5你是15岁而不是15岁10@Sean我现在看到了您的预期输出。我已更新了我的答案以反映这一点。@Sean很高兴提供帮助!请查看我最近的编辑。当显示不同的输入列表时,解决方案确实有效。嘿,抱歉,我认为我对所需输出的描述不太清楚。所需输出将是[['P1'],20],['P2'],20],['P3'],10],['P4'],0],['P5'],0]]嘿,我已经尝试过了,但是它似乎只更新了“totalRank”列表中的前x个玩家数量,其中x是“玩家点数”列表中的玩家数量。因此,例如,如果“玩家点数”列表是:[[P1,10],[P19,10]],它会将“totalRank”列表更新为:[[P1],10][[P2],10],[P3,10]]@肖恩:嗯,如果没有字典,这段代码就更难了。你确定totalRank或playerPoints不能是字典,或者两者都必须是列表吗?它可能与字典一起工作。是的,我只是不太确定自己该怎么做,因为我没有很好地编程long@Sean字典将使程序更加简单,我现在正在用字典编辑答案。嘿,非常感谢您的帮助!我现在正在尝试更改代码中的一些内容,使我能够将这些实现为字典而不是列表