学习Python的列表生成器以及Map-Reduce

最近写了很多的接口,需要反复处理json数据,抽离数据,重组格式。
花时间研究了一些python的列表生成,以及更高级的Map/Reduce, 并给大家分享了一下,记录一下share的过程写下的代码。

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from functools import reduce

datas = [
{
"date": "22日星期四",
"sunrise": "06:17",
"high": "高温 17.0℃",
"low": "低温 1.0℃",
"sunset": "18:27",
"aqi": 98,
"fx": "西南风",
"fl": "<3级",
"type": "晴",
"notice": "愿你拥有比阳光明媚的心情"
},
{
"date": "23日星期五",
"sunrise": "06:16",
"high": "高温 18.0℃",
"low": "低温 5.0℃",
"sunset": "18:28",
"aqi": 118,
"fx": "无持续风向",
"fl": "<3级",
"type": "多云",
"notice": "阴晴之间,谨防紫外线侵扰"
},
{
"date": "24日星期六",
"sunrise": "06:14",
"high": "高温 21.0℃",
"low": "低温 7.0℃",
"sunset": "18:29",
"aqi": 52,
"fx": "西南风",
"fl": "<3级",
"type": "晴",
"notice": "愿你拥有比阳光明媚的心情"
},
{
"date": "25日星期日",
"sunrise": "06:13",
"high": "高温 22.0℃",
"low": "低温 7.0℃",
"sunset": "18:30",
"aqi": 71,
"fx": "西南风",
"fl": "<3级",
"type": "晴",
"notice": "愿你拥有比阳光明媚的心情"
},
{
"date": "26日星期一",
"sunrise": "06:11",
"high": "高温 21.0℃",
"low": "低温 8.0℃",
"sunset": "18:31",
"aqi": 97,
"fx": "西南风",
"fl": "<3级",
"type": "多云",
"notice": "阴晴之间,谨防紫外线侵扰"
}
]


def add(x):
return x + 'Z'


def sum(x, y):
return x + y


if __name__ == '__main__':
zz = []
for data in datas:
zz.append(data['date'] + data['high'] + data['low'])
print(zz)

p = 9
a = 10
zz = p if p > a else a
print(zz)

zz = [data['date'] + data['high'] + data['low'] for data in datas if data["aqi"] > 90]
print(zz)

zz = ['不好' if data["aqi"] > 90 else '不好' for data in datas]
print(zz)

zz = [{data['date']: data['high']} for data in datas]
print(zz)

# zz = {data['date']: data['high'],data['sunrise']: data['sunset'] for data in datas}
# print(zz)

# MAP
list_a = ['A', 'B', 'C']

list_b = [1, 2, 3]
# map -- ['A+','B+','C+']
# reduce -- D= 'A+'+'B+'+'C+'

zz = list(map(add, list_a))
print(zz)

zz = reduce(sum, list_b, 6)
print(zz)

zz = list(map(lambda x: x + 'Z', list_a))
print(zz)

zz = reduce(lambda x, y: x + y, list_b, 6)
print(zz)

zz = list(
map(lambda x: {x['date']: x['fx'], x['aqi']: x['sunset'], 'status': '好天气' if x['aqi'] < 90 else '坏天气'},
datas))
print(zz)

zz = []
for x in datas:
d = {x['date']: x['fx'], x['aqi']: x['sunset']}
if x['aqi'] < 90:
d['status'] = '好天气'
else:
d['status'] = '坏天气'
zz.append(d)
print(zz)

zz = reduce(lambda d, k: {**d, **{k['date']: k['fx'], k['sunrise']: k['sunset']}}, datas, {})
print(zz)

a = {'22日星期四': '西南风', 98: '18:27', 'status1': '坏天气'}
b = {'23日星期五': '无持续风向', 118: '18:28', 'status2': '天气'}

print(a.update(b))
print({**a, **b})

datas = [(3, 5), (6, 8), (1, 9), (4, 8)]

zz = reduce(lambda d, k: (d[0] + k[0], d[1] + k[1]), datas, (0, 0))
print(zz)

Notice: 正常情况下,这里会有一个基于utteranc.es的留言系统,如果看不到,可能需要科学上网方式。