0

So I am trying to create a table system that can show the users the current tables, scores, games, fixtures etc and thought it might be easier to determine the order as they get put into the json file but not actually order them until they have to be displayed.

The issue im having is that I cant figure out how to detect where the position is the correct one or not.

The code I have so far is:

def showtbl(teams, sport, league):
  teamdata = teams[sport.get()][league.get()]
  
  w = Tk()

  Label(w, text="POS TEAM PLD W D L PF PA PD PTS").grid(row=0, column=0, columnspan=10)

  count = 1
  max = len(teamdata)

  data = []

  while count != max:
    for i in teamdata:
      print(teamdata[i])
      if teamdata[i]["pos"] == count:
        count += 1
        data.append(teamdata)
  
  print(data)

(for now im printing it but soon will get it into a tkinter window)

The data is stored as follows:

{ "Sport": {
    "League": {
      "Team 1":
      {"pos": 2, "pld": 7, "won": 2, "draw": 0, "lost": 5, "pf": 92, "pa": 172, "pd": -80, "pts": 4},
      "Team 2":
      {"pos": 1, "pld": 7, "won": 5, "draw": 0, "lost": 2, "pf": 172, "pa": 92, "pd": 80, "pts": 10}
    }
  }
}

(sport and league have already been determined so teamdata = data[sport][league])

I feel like im close to the solution but I have no idea where to go from here to get this working.

How can I order these as they get displayed?

1 Answers1

1

You're appending teamdata to your data list, which probably isn't what you meant to do. You probably meant to append teamdata[i] instead.

Also, it's not a good idea to sort your teamdata instead of using a (relatively) expensive for-loop inside a while-loop. Try using the sorted function like this:

def showtbl(teams, sport, league):
  teamdata = teams[sport.get()][league.get()]
  w = Tk()
  Label(w, text="POS TEAM PLD W D L PF PA PD PTS").grid(row=0, column=0, columnspan=10)

  data = []
  for team in sorted(teamdata, key=lambda x: x["pos"]): 
    data.append(team)  # < this is the important change

  print(data)

You can see here that I'm appending the team rather than teamdata. I'm sorting your teamdata using a lambda - a kind of one-line function, which tells the sorting function what specific part of the teamdata it should be using to sort.