Skip to content

Commit ba60f75

Browse files
committed
新增列表解析测试
1 parent e9df07a commit ba60f75

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

test/test_list_comprehension.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
"""
5+
@author: zhanghe
6+
@software: PyCharm
7+
@file: test_list_comprehension.py
8+
@time: 2017/7/25 下午5:37
9+
"""
10+
11+
12+
def run():
13+
"""
14+
列表解析
15+
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4}
16+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
17+
[]
18+
[1, 3, 2, 5, 4] # 字典无序
19+
[1, 2, 3, 4, 5]
20+
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
21+
[[0, 0], [1, 1], [2, 4], [3, 9], [4, 16], [5, 25], [6, 36], [7, 49], [8, 64], [9, 81]]
22+
:return:
23+
"""
24+
A0 = dict(zip(('a', 'b', 'c', 'd', 'e'), (1, 2, 3, 4, 5)))
25+
A1 = range(10)
26+
A2 = [i for i in A1 if i in A0]
27+
A3 = [A0[s] for s in A0]
28+
A4 = [i for i in A1 if i in A3]
29+
A5 = {i: i * i for i in A1}
30+
A6 = [[i, i * i] for i in A1]
31+
32+
print A0
33+
print A1
34+
print A2
35+
print A3
36+
print A4
37+
print A5
38+
print A6
39+
40+
41+
if __name__ == '__main__':
42+
run()
43+
pass

0 commit comments

Comments
 (0)