Sean
Sean

Reputation: 157

Python list manipulation and comparison of lists

I am having a bit of trouble working out some code to update ranking points for player scores in a game with the use of lists. The first list I have is a list of positions that the players came in, for example:

results = [P2, P4, P3, P1, P5, P6]

This list is in descending order (P2 came first, P4 came second etc) and is determined by other code that I have in the program. The second list I have is a list of ranking points that I want to assign to each player based on their position:

rankingPoints = [100, 50, 25, 10, 5, 0]

(P2 would get 100, P4 would get 50, etc)

Lastly, I have a third list that contains nested lists for each player:

playerRank = [[P1], [P2], [P3], [P4], [P5], [P6]]

Basically what I'm wanting to do is initialize each player in the 'playerRank' list with '0' ranking score (The players are read into the list from a csv file and I cannot initialize them with '0' manually) so that it looks something like this: [[P1, 0], [P2, 0], [P3, 0]] etc.

Then based on their position in the game, add the appropriate amount of ranking score to their current ranking score (there will be multiple games so ranking score will be constantly added on top of current ranking score for players), the desired outcome will look something like:

playerRank = [[P1, 10] [P2, 100], [P3, 25], [P4, 50], [P5, 5], [P6, 0]]

Any help on this would be greatly appreciated as I am new to programming and struggling to work out the code and logic behind it

Thanks

Upvotes: 1

Views: 237

Answers (2)

Vasilis G.
Vasilis G.

Reputation: 7844

If you want to initialize your players ranks in the beginning and update them whenever is necessary, you can do something like this:

def updateScore(playerRank, rankingPoints):
    for ind, i in enumerate(rankingPoints):
        playerRank[ind][1] = playerRank[ind][1] + rankingPoints[ind]

results = ['P2', 'P4', 'P3', 'P1', 'P5', 'P6']
rankingPoints = [100, 50, 25, 10, 5, 0]

print("Initializing player ranks...")
playerRank = [[results[i],0] for i in range(0,len(results))]
print(playerRank)

print("Updating scores...")
updateScore(playerRank, rankingPoints)
playerRank = sorted(playerRank)
print(playerRank)

Output:

 Initializing player ranks...
[['P2', 0], ['P4', 0], ['P3', 0], ['P1', 0], ['P5', 0], ['P6', 0]]
Updating scores...
[['P1', 10], ['P2', 100], ['P3', 25], ['P4', 50], ['P5', 5], ['P6', 0]]

You can call updateScore any time you want to update your players ranks.

Upvotes: 0

Ajax1234
Ajax1234

Reputation: 71451

You can use a defaultdict to update the scores and zip to create the game results:

from collections import defaultdict
results = ['P2', 'P4', 'P3', 'P1', 'P5', 'P6']
rankingPoints = [100, 50, 25, 10, 5, 0]
d = defaultdict(int)
for a, b in zip(results, rankingPoints):
   d[a] += b

final_results = [[a, b] for a, b in sorted(d.items(), key=lambda x:x[0])]

Output:

[['P1', 10], ['P2', 100], ['P3', 25], ['P4', 50], ['P5', 5], ['P6', 0]]

While you could just use sorted(zip(results, rankingPoints), key=lambda x:x[0]) to achieve the final output, the dictionary will allow you to increment the scores for each player later on in the program.

Upvotes: 1

Related Questions