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

时间:2018-03-13 00:14:03

标签: python list

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

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

我还有第二个类似的名单:

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

我想要做的是根据他们在'playerPoints'列表中的积分更新'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]

这段代码确实有效,但是想要的效果不是将'playerPoints'列表中的点添加到'totalRank'列表中,而是更新它们,或者如果有意义的话,'使它们等于'?因此,例如,下一个'playerPoints'列表可能是:

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]]

任何对此的帮助都会非常感激,因为我现在已经挣扎了很长时间了!

3 个答案:

答案 0 :(得分:1)

你可以试试这个:

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]]

编辑:解决方案可以使用其他输入:

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'], 25], [['P2'], 25], [['P3'], 10], [['P4'], 0], [['P5'], 0]]

答案 1 :(得分: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},依此类推。添加playerPoints时请记住这一点。

答案 2 :(得分:0)

试试这个。它适用于我指定的播放列表值。

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] ..但值有效。