-
Notifications
You must be signed in to change notification settings - Fork 49
/
sorted.py
48 lines (36 loc) · 1.62 KB
/
sorted.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
48
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)))
# int + string sequences (mixed bag)
int_string_seqs = [1, '299', 3, '10', 7]
# print(sorted(int_string_seqs))
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: '<' not supported between instances of 'str' and 'int'
print(sorted(int_string_seqs, key=int))
print(sorted(int_string_seqs, key=str))
# 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])
# euqal
# from operator import itemgetter
# sortedList_random = sorted(random, key=itemgetter(1))
print('sortedList: {}'.format(sortedList))
print('sortedList_random: {}'.format(sortedList_random))