57 lines
1.2 KiB
Python
57 lines
1.2 KiB
Python
# dogs-lists10a
|
|
|
|
#Example using itemgetter
|
|
|
|
from operator import itemgetter
|
|
|
|
#Dog Description List format = [<dogname>,<age>,<sex>, <breed>]
|
|
|
|
dogs = [["Mandy", 2, "F", "Chinook"], ["Harry",11,"M", "Chinook"], ["Shenanigans",8,"F", "Chinook"],
|
|
["Colorado",10,"F", "Chinook"], ["Katrina", 9, "F", "Chinook"], ["Rusty",10.5,"M", "Golden Retriever" ],
|
|
["Tanner", 16, "M", "Golden Retriever"] ]
|
|
|
|
# Poor method to print list
|
|
print(dogs)
|
|
|
|
# Print the list & position
|
|
positionId = 0
|
|
for dog in dogs:
|
|
print("{} {}".format(positionId,dog))
|
|
positionId += 1
|
|
|
|
|
|
# Print males in the list
|
|
print("\n - These are the male dogs")
|
|
positionId = 0
|
|
for dog in dogs:
|
|
if dog[2] == "M":
|
|
print("{} {}".format(positionId,dog))
|
|
positionId += 1
|
|
|
|
print("\n")
|
|
|
|
# Print females in the list
|
|
print("\n - These are the female dogs")
|
|
positionId = 0
|
|
for dog in dogs:
|
|
if dog[2] == "F":
|
|
print("{} {}".format(positionId,dog))
|
|
positionId += 1
|
|
|
|
print("\n")
|
|
|
|
print("Sort by name")
|
|
print(sorted(dogs, key=itemgetter(0)))
|
|
|
|
|
|
print("Sort by sex")
|
|
print(sorted(dogs, key=itemgetter(2)))
|
|
|
|
print("Sort by breed")
|
|
print(sorted(dogs, key=itemgetter(3)))
|
|
|
|
print("Sort by age")
|
|
print(sorted(dogs, key=itemgetter(1)))
|
|
|
|
#z = sorted(dogs, key=itemgetter(1))
|
|
#print(type(z),z)
|