Added lecture 2026113
This commit is contained in:
parent
be356ccb59
commit
5b630435ae
27 changed files with 738 additions and 0 deletions
55
lecture20261103/lists02/dogs-lists10.py
Normal file
55
lecture20261103/lists02/dogs-lists10.py
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
# dogs-lists10
|
||||
|
||||
#Example using lambda
|
||||
|
||||
#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 sex")
|
||||
print(sorted(dogs, key=lambda x: x[2]))
|
||||
|
||||
print("Sort by name")
|
||||
print(sorted(dogs, key=lambda x: x[0]))
|
||||
|
||||
|
||||
print("Sort by age")
|
||||
print(sorted(dogs, key=lambda x: x[1]))
|
||||
|
||||
print("Sort by breed")
|
||||
print(sorted(dogs, key=lambda x: x[3]))
|
||||
|
||||
print("\n")
|
||||
print(dogs)
|
||||
Loading…
Add table
Add a link
Reference in a new issue