forked from twtrubiks/python-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorted.py
More file actions
34 lines (25 loc) 路 1.15 KB
/
Copy pathsorted.py
File metadata and controls
34 lines (25 loc) 路 1.15 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
def sort_by_self(elem):
return elem[1]
if __name__ == "__main__":
# sorted(iterable[, key][, reverse])
# iterable - sequence (string, tuple, list) or collection (set, dictionary, frozen set) or any iterator
# reverse (Optional) - If true, the sorted list is reversed (or sorted in Descending order)
# key (Optional) - function that serves as a key for the sort comparison
pyList = ['c', 'a', 'd', 'e', 'b']
print('sorted(pyList): {}'.format(sorted(pyList)))
print('pyList: {}'.format(pyList))
# string
int_string = '51423'
print('int_string: {}'.format(sorted(int_string)))
# vowels tuple
pyTuple = ('c', 'a', 'd', 'e', 'b')
print('sorted(pyTuple): {}'.format(sorted(pyTuple)))
pySet = {'c', 'a', 'd', 'e', 'b'}
print('sorted(pySet, reverse=True): {}'.format(sorted(pySet, reverse=True)))
# random list
random = [('b', 2), ('d', 4), ('a', 1), ('c', 3)]
# sort list with key
sortedList = sorted(random, key=sort_by_self)
sortedList_random = sorted(random, key=lambda x: x[1])
print('sortedList: {}'.format(sortedList))
print('sortedList_random: {}'.format(sortedList_random))