设为首页 收藏本站
查看: 725|回复: 0

[经验分享] Python核心数据类型之字典15

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-10-27 09:08:30 | 显示全部楼层 |阅读模式
一,字典
    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'}



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-131229-1-1.html 上篇帖子: Python处理大文件 下篇帖子: Python压缩/解压gzip大文件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表