0

I have two Class Variables in Python, e.g. Australians and Americans, they have the same attributes, but are seperate so I can differentiate between the two. I combine them into an array to sort them according age. Now I wish to display them as shown below, but I am struggling to find a way to do it. I am quite new to Python scripting and still learning.

class Aus(object):
    def __init__(self, Name, Age, Height):
        self.Name = Name
        self.Age = Age
        self.Height = Height

class US(object):
    def __init__(self, Name, Age, Height):
        self.Name = Name
        self.Age = Age
        self.Height = Height

John = Aus("John", 22, 153)
Steve = Aus("Steve", 15, 180)
Henry = PC("Henry", 20, 192)
Tim = PC("Tim", 85, 160)
Aussies = [John, Steve, Henry, Tim]

Mary = US("Mary", 21, 178)
Sue = US("Sue", 80, 159)
Americans = [Mary, Sue]

Total = Aussies + Americans
Total.sort(key=lambda x: x.Age, reverse=True)

I wish for the out put to be something like this

Tim -> 85 Years/160 cm
Sue -> 80 Years/159 cm
John -> 22 Years/153 cm
Mary -> 21 Years/178 cm

etc.

Is there a way to achieve this?

Chris
  • 29,127
  • 3
  • 28
  • 51
Rahul
  • 21
  • 1
  • 2
  • your class attributes (name, age,...) should be lower case, see https://stackoverflow.com/questions/159720/what-is-the-naming-convention-in-python-for-variable-and-function-names – LarsVegas Jun 20 '19 at 05:34

2 Answers2

2

One way is to define __str__ of your own:

Note that I assumed Henry and Tim are also Aus:

class Intro(object):
    def __str__(self):
        return "%s -> %s Years/%s cm" % (self.Name, self.Age, self.Height)

class Aus(Intro):
    def __init__(self, Name, Age, Height):
        self.Name = Name
        self.Age = Age
        self.Height = Height

class US(Intro):
    def __init__(self, Name, Age, Height):
        self.Name = Name
        self.Age = Age
        self.Height = Height

John = Aus("John", 22, 153)
Steve = Aus("Steve", 15, 180)
Henry = Aus("Henry", 20, 192)
Tim = Aus("Tim", 85, 160)
Aussies = [John, Steve, Henry, Tim]

Mary = US("Mary", 21, 178)
Sue = US("Sue", 80, 159)
Americans = [Mary, Sue]

Total = Aussies + Americans

Then use sorted with str:

[str(s) for s in sorted(Total, key=lambda x:x.Age, reverse=True)]

Output:

['Tim -> 85 Years/160 cm',
 'Sue -> 80 Years/159 cm',
 'John -> 22 Years/153 cm',
 'Mary -> 21 Years/178 cm',
 'Henry -> 20 Years/192 cm',
 'Steve -> 15 Years/180 cm']
Chris
  • 29,127
  • 3
  • 28
  • 51
0

You have done almost all the work yourself. I have made small changes to your code which i think is the best practice.

class Person(object):
    def __init__(self, Name,Age,Height,Country):
        self.Name = Name
        self.Age = Age
        self.Height = Height
        self.Country = Country

John = Person("John", 22, 153,"Australia")
Steve = Person("Steve", 15, 180,"Australia")
Henry = Person("Henry", 20, 192,"Australia")
Tim = Person("Tim", 85, 160,"Australia")
Aussies = [John, Steve, Henry, Tim]

Mary = Person("Mary", 21, 178,"US")
Sue = Person("Sue", 80, 159,"US")
Americans = [Mary, Sue]

Total = Aussies + Americans
Total.sort(key=lambda x: x.Age, reverse=True)
for i in Total:
    print(i.Name + " -> " + str(i.Age) + " Years/" + str(i.Height) + "cm")

Output

Tim -> 85 Years/160cm
Sue -> 80 Years/159cm
John -> 22 Years/153cm
Mary -> 21 Years/178cm
Henry -> 20 Years/192cm
Steve -> 15 Years/180cm