546324 发表于 2015-10-27 09:08:30

Python核心数据类型之字典15

一,字典
    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 : d1 = {'x':32,'y':}

In : d1['y']
Out:

In : d1['y']
Out:





    4. 常用操作


1
2
3
4
5
In : d1.
d1.clear       d1.get         d1.iteritems   d1.keys      d1.setdefaultd1.viewitems
d1.copy      d1.has_key   d1.iterkeys    d1.pop         d1.update      d1.viewkeys   
d1.fromkeys    d1.items       d1.itervaluesd1.popitem   
d1.values      d1.viewvalues




二,字典的常用操作示例

    1. len取得元素个数

      例如:


1
2
3
4
5
6
7
8
In : len(d1)
Out: 2

In : print d1
{'y': , 'x': 504}

In : d1
Out: {'x': 504, 'y': }




    2.字典的复制

      例如:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
In : d2 = d1.copy()

In : d2
Out: {'x': 504, 'y': }

In : id(d1)
Out: 49109552

In : id(d2)
Out: 48033392

In : d2 = d1

In : id(d2)
Out: 49109552

In : id(d1)
Out: 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 : d1
Out: {'x': 504, 'y': }

In : d1.get('x')
Out: 504

In : d1.get('y')
Out:

In : d1.get('z')

In : d1['x']
Out: 504

In : 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 : d1.has_key('x')
Out: True

In : d1.has_key('z')
Out: False

In : d1.has_key('')
Out: False




    5. 把字典键值都拆成元组列表

      例如:


1
2
In : d1.items()
Out: [('y', ), ('x', 504)]




6.变量解包

      例如:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
In : t1,t2 = d1.items()

In : print t1
('y', )

In : print t2
('x', 504)

In : 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 : m1,m2 = {'x':1,'y':2}

In : print m1
y

In : print m2
x

In : d1
Out: {'x': 504, 'y': }

In : d1.keys()
Out: ['y', 'x']

In : d1.values()
Out: [, 504]




    8.popitem随机弹出键值映射

      例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
In : d1.pop('y')
Out:

In : d1
Out: {'x': 504}

In : d2 = {'x':1,'y':2,'z':3}

In : d2.popitem()
Out: ('y', 2)

In : d2
Out: {'x': 1, 'z': 3}




    9.update合并覆盖键值

      例如:


1
2
3
4
5
6
7
8
9
10
11
In : d1
Out: {'x': 504}

In : d1 = {'x':1,'y':2}

In : d2 = {'m':21,'n':76,'y':44}

In : d1.update(d2)

In : 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 : d1
Out: {'m': 21, 'n': 76, 'x': 1, 'y': 44}


In : help(dict.iteritems)

In : d1.iteritems()
Out: <dictionary-itemiterator at
0x2f03578>                  //返回迭代器的地址

In : i1 = d1.iteritems()

In : i1.next()
Out: ('y', 44)

In : i1.next()
Out: ('x', 1)

In : i1.next()
Out: ('m', 21)

In : i1.next()
Out: ('n', 76)

In : 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 : i2 = d1.iterkeys()

In : i2
Out: <dictionary-keyiterator at 0x2f03cb0>

In : i2.next()
Out: 'y'

In : i2.next()
Out: 'x'

In : i2.next()
Out: 'm'

In : i2.next()
Out: 'n'

In : 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 : i3 = d1.itervalues()

In : i3.next()
Out: 44

In : i3.next()
Out: 1

In : i3.next()
Out: 21

In : i3.next()
Out: 76

In : 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 : d1
Out: {'m': 21, 'n': 76, 'x': 1, 'y': 44}

In : d1.viewitems()
Out: dict_items([('y', 44), ('x', 1), ('m', 21), ('n', 76)])




    14.viewitems显示字典的构造方式
      例如:


1
2
3
4
5
6
7
8
In : d2 = dict(name='jerry',gender='m',age=42)

In : d2.viewitems()
Out: dict_items([('gender', 'm'), ('age', 42), ('name',
'jerry')])

In : d2
Out: {'age': 42, 'gender': 'm', 'name': 'jerry'}




   15.viewkeys显示键
      例如:


1
2
In : d2.viewkeys()
Out: dict_keys(['gender', 'age', 'name'])




    16.viewvalues显示键值
例如:


1
2
In : d2.viewvalues()
Out: dict_values(['m', 42, 'jerry'])




   17.使用zip返回序列
例如:


1
2
3
4
5
6
7
8
9
10
11
In : zip('xyz','123')
Out: [('x', '1'), ('y', '2'), ('z', '3')]

In : zip('xyz','1234')
Out: [('x', '1'), ('y', '2'), ('z', '3')]

In : zip('xyzm','123')
Out: [('x', '1'), ('y', '2'), ('z', '3')]

In : zip('xyz','123','abc')
Out: [('x', '1', 'a'), ('y', '2', 'b'), ('z', '3', 'c')]




    18.使用zip构造字典
例如:

1
2
In : dict(zip('xyz','123'))
Out: {'x': '1', 'y': '2', 'z': '3'}



页: [1]
查看完整版本: Python核心数据类型之字典15