-
Notifications
You must be signed in to change notification settings - Fork 0
/
52_sort.py
47 lines (32 loc) · 1.09 KB
/
52_sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# sort() method = used with lists
# sort() function = used with iterables
# using the sort() method
students = ["Squidware", "Sandy", "Patrick", "Spongebob", "Mr. Krabs"]
students.sort(reverse=True)
for i in students:
print(i)
# using the sort() function for example if its a tuple and not a list
students = ("Squidware", "Sandy", "Patrick", "Spongebob", "Mr. Krabs")
sorted_students = sorted(students, reverse=True) # returns a list but accepts iterable as an argument
for i in sorted_students:
print(i)
# a list of tuples
students = [("Squidward", "F", 60),
("Sandy", "A", 33),
("Patrick", "D", 36),
("Spongebob", "B", 20),
("Mr. Krabs", "C", 78)]
grade = lambda grades: grades[1]
students.sort(key=grade)
for i in students:
print(i)
# sorting a tuple of tuples
students = (("Squidward", "F", 60),
("Sandy", "A", 33),
("Patrick", "D", 36),
("Spongebob", "B", 20),
("Mr. Krabs", "C", 78))
age = lambda age: age[2]
sorted_lists = sorted(students, key=age)
for i in sorted_lists:
print(i)