kunal sharma kunal sharma -4 years ago 194
Python Question

how to print output dictionary values in decreasing order of ranking

I have written code to take input as list of string

if s=="":

but i am not able to process list 'd' to obtain required output
d[0] is storing "Djokovic:Murray:2-6,6-7,7-6,6-3,6-1" now i want to process this string(Either converting into dictionary)

Now i am able write code(by using str.split(",") function) to calculate Number of best-of-5 set matches won, Number of best-of-3 set matches won, Number of sets won, Number of games won, Number of sets lost, Number of games lost

Now i had stored result in dictionary as


Now i want to print output in decreasing order of ranking, where the ranking is according to the criteria 1-6 in that order (compare item 1, if equal compare item 2, if equal compare item 3 etc, noting that for items 5 and 6 the comparison is reversed).

print out the following from playerStats dictionary.....

Djokovic 3 1 13 142 16 143

Murray 2 2 16 143 13 142

Williams 0 1 2 15 1 12

Muguruza 0 0 1 12 2 15

where criteria 1-6 are... 1. Number of best-of-5 set matches won 2. Number of best-of-3 set matches won 3. Number of sets won 4. Number of games won 5. Number of sets lost 6. Number of games lost

I had tried this code

if playerStats[playerNames[n]][0]>playerStats[playerNames[n+1]][0] and playerStats[playerNames[n]][0]>playerStats[playerNames[n+2]][0] and playerStats[playerNames[n]][0]>playerStats[playerNames[n+3]][0]:
if playerStats[playerNames[n+1]][0]>playerStats[playerNames[n+2]][0] and playerStats[playerNames[n+1]][0]>playerStats[playerNames[n+3]][0]:
if playerStats[playerNames[n+1]][0]>playerStats[playerNames[n+2]][0] and playerStats[playerNames[n+1]][0]>playerStats[playerNames[n]][0] and playerStats[playerNames[n+1]][0]>playerStats[playerNames[n+3]][0]:

Answer Source

Since your stats seem to be separated by a whitespace delimiter, you can use str.split() to separate each of your stats into a list. here is a demo:

stats = "Djokovic:Murray:2-6,6-7,7-6,6-3,6-1 Murray:Djokovic:6-3,4-6,6-4,6-3"

def compile_stats(stats):
    stats_lst = list(stats.split(" ")) # using str.split to 
    # split the string every time whitespace is found.
    return stats_lst 

# output:['Djokovic:Murray:2-6,6-7,7-6,6-3,6-1', 'Murray:Djokovic:6-3,4-6,6-4,6-3']

It takes each stat, and makes it its own separate list item.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download