-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLambdaSorting.py
executable file
·75 lines (58 loc) · 1.39 KB
/
LambdaSorting.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 25 11:57:21 2021
@author: maherme
"""
#%%
# How sorted works?:
l = [1, 5, 4, 10, 9, 6]
a = sorted(l)
print(a)
print(l) # l is not changed by sorted
#%%
l = ['c', 'B', 'D', 'a']
a = sorted(l)
print(a)
print("---------------------------")
a = sorted(l, key=lambda s: s.upper()) # We can do sorted case insensitive
print(a)
#%%
d = {'def': 300, 'abc': 200, 'ghi': 100}
print(d)
for e in d:
print(e)
a = sorted(d)
print(a)
a = sorted(d, key=lambda e: d[e]) # Now we order by value, not by key
print(a)
#%%
def dist_sq(x):
return (x.real)**2 + (x.imag)**2
a = dist_sq(1+1j)
print(a)
l = [3+3j, 1-1j, 0, 3]
a = sorted(l, key=dist_sq) # sorted does not work with complex number, but we can use a lambda to sort them
print(a)
a = sorted(l, key=lambda x: (x.real)**2 + (x.imag)**2)
print(a)
#%%
l = ['Cleese', 'Idle', 'Palin', 'Chapman', 'Guilliam', 'Jones']
print(l)
a = sorted(l)
print(a)
a = sorted(l, key=lambda s: s[-1]) # Sorted by the last character
print(a) # If a tie exists, python keep the original order to decide what goes first
#%%
l = ['Idle', 'Cleese', 'Palin', 'Chapman', 'Guilliam', 'Jones']
print(l)
a = sorted(l, key=lambda s: s[-1])
print(a)
#%%
# Let's try to shuffle a list using lambdas and random:
import random
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(l)
a = sorted(l, key=lambda x: random.random())
print(a)
#%%