forked from zhanghe06/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_dict.py
More file actions
55 lines (42 loc) · 988 Bytes
/
test_dict.py
File metadata and controls
55 lines (42 loc) · 988 Bytes
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
# encoding: utf-8
"""
将两个list组合为dict
"""
__author__ = 'zhanghe'
def test_01():
"""
key value 数量相等
"""
list_a = ['a', 'b', 'c']
list_b = ['10', '20', '30']
print dict(zip(list_a, list_b))
print dict(zip(list_a[::-1], list_b))
def test_02():
"""
key 数量小于 value
"""
list_a = ['a', 'b', 'c']
list_b = ['10', '20', '30', '50']
print dict(zip(list_a, list_b))
print dict(zip(list_a[::-1], list_b))
def test_03():
"""
key 数量大于 value
"""
list_a = ['a', 'b', 'c', 'd']
list_b = ['10', '20', '30']
print dict(zip(list_a, list_b))
print dict(zip(list_a[::-1], list_b))
if __name__ == '__main__':
test_01()
test_02()
test_03()
"""
测试结果:
{'a': '10', 'c': '30', 'b': '20'}
{'a': '30', 'c': '10', 'b': '20'}
{'a': '10', 'c': '30', 'b': '20'}
{'a': '30', 'c': '10', 'b': '20'}
{'a': '10', 'c': '30', 'b': '20'}
{'c': '20', 'b': '30', 'd': '10'}
"""