|
一,字典
1. 字典在其它编程语言中又称为关联数组或散列表;
2. 特性:
a. 通过键值实现元素存取;
b. 无序集合;
c. 可变类型容器;
d. 长度可变;
e. 支持异构;
f. 支持嵌套;
3. 语法
a. d1 = {key1:value1, key2:value2, ...} ;
b. d1 = {} //表示空字典;
例如:
1
2
3
4
5
6
7
| In [435]: d1 = {'x':32,'y':[1,2,3]}
In [440]: d1['y'][2:]
Out[440]: [3]
In [441]: d1['y'][1:]
Out[441]: [2, 3]
|
4. 常用操作
1
2
3
4
5
| In [448]: d1.[table]
d1.clear d1.get d1.iteritems d1.keys d1.setdefault d1.viewitems
d1.copy d1.has_key d1.iterkeys d1.pop d1.update d1.viewkeys
d1.fromkeys d1.items d1.itervalues d1.popitem
d1.values d1.viewvalues
|
二,字典的常用操作示例
1. len取得元素个数
例如:
1
2
3
4
5
6
7
8
| In [445]: len(d1)
Out[445]: 2
In [446]: print d1
{'y': [1, 2, 3], 'x': 504}
In [447]: d1
Out[447]: {'x': 504, 'y': [1, 2, 3]}
|
2.字典的复制
例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| In [449]: d2 = d1.copy()
In [450]: d2
Out[450]: {'x': 504, 'y': [1, 2, 3]}
In [451]: id(d1)
Out[451]: 49109552
In [452]: id(d2)
Out[452]: 48033392
In [453]: d2 = d1
In [454]: id(d2)
Out[454]: 49109552
In [455]: id(d1)
Out[455]: 49109552
|
3. get取得对应键的值
例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| In [456]: d1
Out[456]: {'x': 504, 'y': [1, 2, 3]}
In [457]: d1.get('x')
Out[457]: 504
In [458]: d1.get('y')
Out[458]: [1, 2, 3]
In [459]: d1.get('z')
In [461]: d1['x']
Out[461]: 504
In [462]: d1['z']
---------------------------------------------------------------------------
KeyError Traceback
(most recent call last)
<ipython-input-462-f70e6eeb835b> in <module>()
----> 1 d1['z']
KeyError: 'z'
|
4.判断元素
例如:
1
2
3
4
5
6
7
8
| In [463]: d1.has_key('x')
Out[463]: True
In [464]: d1.has_key('z')
Out[464]: False
In [465]: d1.has_key('')
Out[465]: False
|
5. 把字典键值都拆成元组列表
例如:
1
2
| In [466]: d1.items()
Out[466]: [('y', [1, 2, 3]), ('x', 504)]
|
6.变量解包
例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| In [469]: t1,t2 = d1.items()
In [471]: print t1
('y', [1, 2, 3])
In [472]: print t2
('x', 504)
In [474]: t1,t2,t3 = d1.items()
---------------------------------------------------------------------------
ValueError Traceback (most
recent call last)
<ipython-input-474-202a1b3eb745> in <module>()
----> 1 t1,t2,t3 = d1.items()
ValueError: need more than 2 values to unpack
|
7.键和键值
例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| In [477]: m1,m2 = {'x':1,'y':2}
In [478]: print m1
y
In [479]: print m2
x
In [480]: d1
Out[480]: {'x': 504, 'y': [1, 2, 3]}
In [481]: d1.keys()
Out[481]: ['y', 'x']
In [482]: d1.values()
Out[482]: [[1, 2, 3], 504]
|
8.popitem随机弹出键值映射
例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
| In [486]: d1.pop('y')
Out[486]: [1, 2, 3]
In [487]: d1
Out[487]: {'x': 504}
In [488]: d2 = {'x':1,'y':2,'z':3}
In [489]: d2.popitem()
Out[489]: ('y', 2)
In [490]: d2
Out[490]: {'x': 1, 'z': 3}
|
9.update合并覆盖键值
例如:
1
2
3
4
5
6
7
8
9
10
11
| In [495]: d1
Out[495]: {'x': 504}
In [496]: d1 = {'x':1,'y':2}
In [497]: d2 = {'m':21,'n':76,'y':44}
In [498]: d1.update(d2)
In [499]: print d1
{'y': 44, 'x': 1, 'm': 21, 'n': 76}
|
10.iteritems返回迭代器对象
用于遍历对象中的每一个元素。
例如:
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
| In [507]: d1
Out[507]: {'m': 21, 'n': 76, 'x': 1, 'y': 44}
In [500]: help(dict.iteritems)
In [501]: d1.iteritems()
Out[501]: <dictionary-itemiterator at
0x2f03578> //返回迭代器的地址
In [512]: i1 = d1.iteritems()
In [513]: i1.next()
Out[513]: ('y', 44)
In [514]: i1.next()
Out[514]: ('x', 1)
In [515]: i1.next()
Out[515]: ('m', 21)
In [516]: i1.next()
Out[516]: ('n', 76)
In [517]: i1.next()
---------------------------------------------------------------------------
StopIteration Traceback (most
recent call last)
<ipython-input-517-be7912f76fe0> in <module>()
----> 1 i1.next()
StopIteration:
|
11.iterkeys迭代键
例如:
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
| In [519]: i2 = d1.iterkeys()
In [520]: i2
Out[520]: <dictionary-keyiterator at 0x2f03cb0>
In [521]: i2.next()
Out[521]: 'y'
In [522]: i2.next()
Out[522]: 'x'
In [523]: i2.next()
Out[523]: 'm'
In [524]: i2.next()
Out[524]: 'n'
In [525]: i2.next()
---------------------------------------------------------------------------
StopIteration Traceback (most
recent call last)
<ipython-input-525-26e3ebe37329> in <module>()
----> 1 i2.next()
StopIteration:
|
12.itemvalues迭代键值
例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| In [526]: i3 = d1.itervalues()
In [527]: i3.next()
Out[527]: 44
In [528]: i3.next()
Out[528]: 1
In [529]: i3.next()
Out[529]: 21
In [530]: i3.next()
Out[530]: 76
In [531]: i3.next()
---------------------------------------------------------------------------
StopIteration Traceback (most
recent call last)
<ipython-input-531-a0267d328718> in <module>()
----> 1 i3.next()
StopIteration:
|
13. viewitems显示键值对拆分的元组
例如:
1
2
3
4
5
| In [536]: d1
Out[536]: {'m': 21, 'n': 76, 'x': 1, 'y': 44}
In [538]: d1.viewitems()
Out[538]: dict_items([('y', 44), ('x', 1), ('m', 21), ('n', 76)])
|
14.viewitems显示字典的构造方式
例如:
1
2
3
4
5
6
7
8
| In [533]: d2 = dict(name='jerry',gender='m',age=42)
In [534]: d2.viewitems()
Out[534]: dict_items([('gender', 'm'), ('age', 42), ('name',
'jerry')])
In [535]: d2
Out[535]: {'age': 42, 'gender': 'm', 'name': 'jerry'}
|
15.viewkeys显示键
例如:
1
2
| In [541]: d2.viewkeys()
Out[541]: dict_keys(['gender', 'age', 'name'])
|
16.viewvalues显示键值
例如:
1
2
| In [543]: d2.viewvalues()
Out[543]: dict_values(['m', 42, 'jerry'])
|
17.使用zip返回序列
例如:
1
2
3
4
5
6
7
8
9
10
11
| In [544]: zip('xyz','123')
Out[544]: [('x', '1'), ('y', '2'), ('z', '3')]
In [545]: zip('xyz','1234')
Out[545]: [('x', '1'), ('y', '2'), ('z', '3')]
In [546]: zip('xyzm','123')
Out[546]: [('x', '1'), ('y', '2'), ('z', '3')]
In [547]: zip('xyz','123','abc')
Out[547]: [('x', '1', 'a'), ('y', '2', 'b'), ('z', '3', 'c')]
|
18.使用zip构造字典
例如:
1
2
| In [549]: dict(zip('xyz','123'))
Out[549]: {'x': '1', 'y': '2', 'z': '3'}
|
|
|