Miracle
Miracle

Reputation: 115

How to loop and count which playerid have the most score?

   player_score = [['P001', '1', '0', '-1', '503', '1'], ['P067', '1', '1', '0', '-1', '503'], ['P218', '0', '1', '1', '-1', '-1'], ['P101', '0', '0', '1', '1', '503'], ['P456', '1', '1', '-1', '1', '-1']]

My code:

scores_table = {"1" : 1, "0" : -1}

# use the key parameter of max to compute the score
res = max(player_score, key=lambda x: sum(scores_table[i] for i in x[1:] if i in scores_table))[0]
print(res)

I only able to get the playerid, not sure how to store the total win and loses as well.

I would like to find the player with the most score and print their player id, total wins and total loses

Expected result:

The top player is P456 with 3 wins and 0 loses.

Upvotes: 0

Views: 157

Answers (4)

areobe
areobe

Reputation: 167

Try something like this:

max_player = ["P0",0]
for p in player_score:
    t_max = 0
    for s in p:
        if "P" in s or '503' in s:
            continue
        else:
            t_max += int(s)
    if t_max > max_player[1]:
        max_player[1] = t_max
        max_player[0] = p[0]

Upvotes: 0

MK14
MK14

Reputation: 506

You can also convert the score, win count, and loss count in a dictionary and try getting the maximum score out of it like the following :-

player_score = [['P001', '1', '0', '-1', '503', '1'],
                ['P067', '1', '1', '0', '-1', '503'],
                ['P218', '0', '1', '1', '-1', '-1'],
                ['P101', '0', '0', '1', '1', '503'],
                ['P456', '1', '1', '-1', '1', '-1']]

DStats = dict()

for player_stats in player_score:
    score, wins, loses = 0, 0, 0

    for i in player_stats:
        if "P" in i:
            PID = i
        elif i == '1':
            wins += 1
            score += 1
        elif i == '0':
            loses += 1
            score -= 1
    else:
        DStats[PID] = (score, wins, loses)

max_score = max([i[0] for i in DStats.values()])

for i in DStats:
    if DStats[i][0] == max_score:
        print(f"the top player is {i} with {DStats[i][1]} wins and {DStats[i][2]} loses")

What happens here is that, I counted the wins, loses and the score accordingly and stored them in a dictionary with the Player's ID as its key, then I iterate through the values of the dictionary and get the maximum score, then I iterate through the dictionary to find the player with the maximum score and print his stats accordingly.

Upvotes: 0

I'mahdi
I'mahdi

Reputation: 24049

IIUC, you can find player and use Counter and print like waht you want:

>>> from collections import Counter
>>> lst_res = next(ps for ps in player_score if ps[0] == res)
>>> lst_res
['P456', '1', '1', '-1', '1', '-1']

>>> dct_res = Counter(lst_res)
>>> print(f"The top player is {res} with {dct_res.get('1', 0)} wins and {dct_res.get('0',0)} loses")
The top player is P456 with 3 wins and 0 loses

Upvotes: 1

Pedro Maia
Pedro Maia

Reputation: 2732

sums = []
for i in player_score:
    for j in i[1:]:
        score = 0
        if j != "503":
            score += int(j)
    sums.append(score)

winner =  player_score[sums.index(max(sums))][0]

Upvotes: 0

Related Questions