-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChapter 55 - sort.py
More file actions
57 lines (43 loc) · 1.4 KB
/
Copy pathChapter 55 - sort.py
File metadata and controls
57 lines (43 loc) · 1.4 KB
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
48
49
50
51
52
53
54
55
56
57
# CHAPTER 55
# sort
# sort() = sort with lists
# sorted() = sort with iterables(tuples, dictionary, sets)
students = ["Embre", "ABEN", "Meracabe", "Hakdog"]
# PART 1 - Sorting with Lists
# sort the students list ascending
students.sort()
print(students)
print() # for space
# sort the students list descending
students.sort(reverse=True)
print(students)
# PART 2 - Sorting with Iterables
students_iterables = [
("Embre", "F", 75),
("ABEN", "A", 95),
("Meracabe", "B", 90),
("Hakdog", "C", 85),
]
print()
# Sort with Students Names
students_iterables.sort()
print(*students_iterables, sep="\n")
print()
# Sort with Remarks
# Key = get the grades for every tuple
# then sort it
# Remember that sorted is an example of higher-order function
# With an argument of a function
sorted_with_remarks = sorted(students_iterables, key=lambda remarks: remarks[1])
print(*sorted_with_remarks, sep="\n")
print()
# Sort with Grades
sorted_with_grades = sorted(students_iterables, key=lambda grades: grades[2])
print(*sorted_with_grades, sep="\n")
print()
# Sort with Remarks but descending
sorted_with_remarks_descending = sorted(students_iterables, key=lambda remarks: remarks[1], reverse=True)
print(*sorted_with_remarks_descending, sep="\n")
# ANOTHER NOTE:
# When you sort with sort() method, no need to store it with variable.
# Whereas, for sorted() function you need to store the result in a variable