Python中的小技巧

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
#coding:utf-8


# NO.1
def dispatch_if(operator, x, y):
if operator == 'add':
return x + y
elif operator == 'sub':
return x - y
elif operator == 'mul':
return x * y
elif operator == 'div':
return x / y
else:
return None


def dispatch_dict(operator, x, y):
return {
'add': lambda: x + y,
'sub': lambda: x - y,
'mul': lambda: x * y,
'div': lambda: x / y,
}.get(operator, lambda: None)()


print(dispatch_if('sub', 5, 9))
print(dispatch_dict('mul', 3, 7))
print(dispatch_if('unknown', 2, 8))
print(dispatch_dict('unknown', 2, 8))


# NO.2
def myfunc(a, b):
return a + b

funcs = [myfunc]
print(funcs[0])
print(funcs[0](2, 3))


# NO.3
my_mapping = {'a': 23, 'b': 42, 'c': 0xc0ffee}
print(my_mapping)

import json
print(json.dumps(my_mapping, indent=4, sort_keys=True))

# json.dumps({all: 'yup'})


# NO.4
name_for_userid = {
382: "Alice",
590: "Bob",
951: "Dilbert",
}

def greeting(userid):
return "Hi %s!" % name_for_userid.get(userid, "there")

print(greeting(951))

# NO.5
xs = {'a': 4, 'b': 3, 'c': 2, 'd': 1}

S = (sorted(xs.items(), key=lambda x: x[1]))
print(S)
# Or:

import operator
S = sorted(xs.items(), key=operator.itemgetter(1))
print(S)