Dec-11-2017, 07:45 PM
Hello:
I have a simple question, the following is my code:
1. Sort by the Age, and print the person with the max/min age
2. Sort by the Height, and print the person with the max/min height
3. Sort by the Weight, and print the person with the max/min weight
How I can code this?
A furuther question: if you think other data type, not the named tuple could be better for my question, please
also advice and show me your code. I will use this kind of sort quite often in my program, so I wish a good
performance solution.
Please advise,
Thanks,
I have a simple question, the following is my code:
import collections Person = collections.namedtuple('Person', ['Name', 'Age', 'Height', 'Weight']) person1 = Person(name='Bob', Age = 55, Height = 165, Weight = 70) person2 = Person(name='Doe', Age = 45, Height = 185, Weight = 90) person3 = Person(name='John', Age = 50, Height = 175, Weight = 85) people = [] people.append(person1) people.append(person2) people.append(person3)I want to sort the tuple list by the following 3 ways:
1. Sort by the Age, and print the person with the max/min age
2. Sort by the Height, and print the person with the max/min height
3. Sort by the Weight, and print the person with the max/min weight
How I can code this?
A furuther question: if you think other data type, not the named tuple could be better for my question, please
also advice and show me your code. I will use this kind of sort quite often in my program, so I wish a good
performance solution.
Please advise,
Thanks,