Reputation: 531
I am very new to Python so please bear with me if you can. I have created a class called team
that has 4 attributes (city, teamname, wins, losses)
. I've created 5 instances of team
(team_1, team_2, team_3, team_4, team_5)
and have initialized their attributes. See the code below. I am looking for a way to loop through the instances and produce an output that lists the teams by ascending order of wins. I'm not sure if the best way to do this would be to somehow create a list or dictionary from the instances or if there is a more efficient way to loop through the instances. Again I am extremely green when it comes to Python and would very much appreciate any help I can get. Thank you in advance for your time!
class team:
def __init__(self, city, teamname, wins, losses):
self.city = city
self.teamname = teamname
self.wins = wins
self.losses = losses
team_1 = team('Boston','Red Sox', 162, 0)
team_2 = team('New York', 'Yankees', 0, 162)
team_3 = team('Tampa Bay', 'Rays', 80, 82)
team_4 = team('Toronto', 'Blue Jays', 82, 80)
team_5 = team('Baltimore', 'Orioles', 1, 161)
Upvotes: 0
Views: 86
Reputation: 3232
You can define the operator <
(less than) in a class like this:
class Team:
def __init__(self, city, teamname, wins, losses):
self.city = city
self.teamname = teamname
self.wins = wins
self.losses = losses
def __lt__(self, other):
return self.wins < other.wins
def __repr__(self):
return self.teamname # or what you want to show
This way python knows how to sort a list of objects from this class.
sorted_list = sorted([team_1, team_2, team_3, team_4, team_5])
Also by defining the representation operator python knows what to show when the print function is called upon one of these objects.
Upvotes: 1
Reputation: 3447
As many have mentioned in the comments, Creating a list and iterating through is the most simple way to go about it.
team_list = []
team_list.append(team('Boston','Red Sox', 162, 0))
team_list.append(team('Boston','Red Sox', 162, 0))
team_list.append(team('New York', 'Yankees', 0, 162))
team_list.append(team('Tampa Bay', 'Rays', 80, 82))
team_list.append(team('Toronto', 'Blue Jays', 82, 80))
team_list.append(team('Baltimore', 'Orioles', 1, 161))
# For sorting the teams
sorted_team = sorted(team_list, key=lambda team: team.wins)
print([team.city for team in sorted_team])
Upvotes: 1
Reputation: 4265
Python has built in sorting with sorted
https://docs.python.org/3/tutorial/datastructures.html#more-on-lists
You can pass it a key. So here we go:
class Team: # Capitalize your class names
def __init__(self, city, teamname, wins, losses):
self.city = city
self.teamname = teamname
self.wins = wins
self.losses = losses
team_list = [Team('Boston','Red Sox', 162, 0),
Team('New York', 'Yankees', 0, 162),
Team('Tampa Bay', 'Rays', 80, 82),
Team('Toronto', 'Blue Jays', 82, 80),
Team('Baltimore', 'Orioles', 1, 161)]
def sort_key(x):
return x.wins
teams_sorted = sorted(team_list, key=sort_key)
print([x.city for x in teams_sorted])
# ['New York', 'Baltimore', 'Tampa Bay', 'Toronto', 'Boston']
Upvotes: 1
Reputation: 33275
Create an empty list, then append()
each new team object onto the list:
teams = []
teams.append(team('Boston','Red Sox', 162, 0))
teams.append(team('New York', 'Yankees', 0, 162))
teams.append(team('Tampa Bay', 'Rays', 80, 82))
teams.append(team('Toronto', 'Blue Jays', 82, 80))
teams.append(team('Baltimore', 'Orioles', 1, 161))
Upvotes: 0