Sean
Sean

Reputation: 157

Python updating global list based on another list

I'm struggling to re-work a bit of code that I have to perform a slightly different function. Basically I have a list:

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

I also have a second similar list:

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

What I am wanting to do is update the players in the list 'totalRank' based on their points in the 'playerPoints' list, the desired outcome is:

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

Below is the code I have to do this:

global totalRank
totalRank = [[[a], b+dict(playerPoints).get(a, 0)] for [a], b in totalRank]

This code does work however, the desired effect is not to add the points from the 'playerPoints' list to the 'totalRank' list but to update them or 'make them equal to' if that makes sense? So for example, the next 'playerPoints' list to come through may be:

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

and the desired outcome for the 'totalRank' list is:

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

Whereas currently my code would produce the list:

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

Any help on this would be greatly appreciated as I have been struggling for quite a while now!

Upvotes: 0

Views: 63

Answers (3)

hikerjobs
hikerjobs

Reputation: 386

Try this. It works for me with your specified values of 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)

Sample output from above run using your inputs:

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

Note that the [[['P1',x],... has changed to [['P1',x].. but the values are valid.

Upvotes: 0

jaguar
jaguar

Reputation: 152

This should work:

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)

This will have the output:

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

The power of dictionaries is that this code will work for any key that you enter into playerPoints. The format of a dictionary is {key1 : value1, key2 : value2, key3 : value3} and so on. Keep this in mind when you are adding playerPoints.

Upvotes: 0

Ajax1234
Ajax1234

Reputation: 71451

You can try this:

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] 

Output:

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

Edit: the solution does work with additional input:

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] 

Output:

[[['P1'], 25], [['P2'], 25], [['P3'], 10], [['P4'], 0], [['P5'], 0]]

Upvotes: 1

Related Questions