-2

How do I sort dictionary by some date values in a list by python?

This is my dictionary:

{'X': [1, 1, 1, 0, 4], 'Y': [1, 1, 1, 0, 4], 'Z': [1, 1, 1, 0, 4], 'B': [1, 1, 1, 0, 4]}

I want print it like this:

X  wins:1 , loses:0 , draws:2 , goal difference:2 , points:5
Y  wins:1 , loses:1 , draws:1 , goal difference:0 , points:4
Z  wins:1 , loses:1 , draws:1 , goal difference:0 , points:4
B  wins:1 , loses:2 , draws:0 , goal difference:-2 , points:3

In dictionary values the list is like:[wins, loses, draws, goal difference, points]

And I want to sort it by points, if points are equal by wins, and if both are equal by key name(A, Y, Z or B).

ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
  • 3
    The input and the expected output doesn't really match. Also that's not what you call sorting. Its just restructuring or pretty printing. – edusanketdk May 06 '21 at 12:27
  • Please show what you already tried, and where the problem occured. – Andreas May 06 '21 at 12:28
  • Does this answer your question? [How do I sort a dictionary by value?](https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value) – Rafael-WO May 06 '21 at 12:49

4 Answers4

0

Something like:

dic = {'X': [1, 1, 1, 0, 4], 'Y': [1, 1, 1, 0, 4], 'Z': [1, 1, 1, 0, 4], 'B': [1, 1, 1, 0, 4]}

for k, v in dic.items():
    # note: \t is a tab spacing
    print(k, "\twins:", v[0], "\tlosses:", v[1], "\tdraws:", v[2], "\tgoal difference:", v[3], "\tpoints:", v[4])

>>X     wins: 1     losses: 1   draws: 1    goal difference: 0  points: 4
>>Y     wins: 1     losses: 1   draws: 1    goal difference: 0  points: 4
>>Z     wins: 1     losses: 1   draws: 1    goal difference: 0  points: 4
>>B     wins: 1     losses: 1   draws: 1    goal difference: 0  points: 4
Hajny
  • 79
  • 1
  • 4
0

code:

dct = {'X': [1, 1, 1, 0, 4], 'Y': [1, 1, 1, 0, 4], 'Z': [1, 1, 1, 0, 4], 'B': [1, 1, 1, 0, 4]}
for i in dct:
    print("{}, wins:{}, loses:{}, draws:{}, goal difference:{}, points:{}".format(i, dct[i][0], dct[i][1], dct[i][2], dct[i][3], dct[i][4]))

output:

X, wins:1, loses:1, draws:1, goal difference:0, points:4
Y, wins:1, loses:1, draws:1, goal difference:0, points:4
Z, wins:1, loses:1, draws:1, goal difference:0, points:4
B, wins:1, loses:1, draws:1, goal difference:0, points:4

Traversing the dictionary by keys and using print formatting is the way to achieve the expected output.

edusanketdk
  • 602
  • 1
  • 6
  • 11
0

I would recommend using pandas, because it has built-in functionality for this kind of data handling, e.g.

import pandas as pd

d = {'X': [1, 1, 1, 0, 2], 'Y': [1, 1, 1, 0, 2], 'Z': [1, 1, 1, 0, 4], 'B': [2, 1, 1, 0, 4]}
df = pd.DataFrame(d).T
df.reset_index(inplace=True)
df.columns = ['name', 'wins', 'losses', 'draws', 'goal difference', 'points']

df.sort_values(['points', 'wins', 'name'], ascending=False)
    name    wins    losses  draws   goal difference     points
3   B       2       1       1       0                   4
2   Z       1       1       1       0                   4
1   Y       1       1       1       0                   2
0   X       1       1       1       0                   2
Arne
  • 9,990
  • 2
  • 18
  • 28
0

Thank you for your answers If try this code for my best answer:

dic={'X': [1, 1, 1, 1, 4], 'Y': [2, 1, 0, 1, 6], 'Z': [1, 2, 0, 0, 3], 'B': [1, 1, 1, 1, 4]}
dct=sorted(sorted(dic.items(), key = lambda x : x[0]), key = lambda x : (x[1][4], x[1][0]), reverse = True)
for a,b in dct:
        print("{}  wins:{} , loses:{} , draws:{} , goal difference:{} , points:{}".format(a, b[0], b[1], b[2], b[3], b[4]))

output is:

Y  wins:2 , loses:1 , draws:0 , goal difference:1 , points:6
B  wins:1 , loses:1 , draws:1 , goal difference:1 , points:4
X  wins:1 , loses:1 , draws:1 , goal difference:1 , points:4
Z  wins:1 , loses:2 , draws:0 , goal difference:0 , points:3

This code is sorted points at first. if some points are equal sort wins and then sort Names as alphabet.