• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python中字典(Dictionary)用法实例详解

python中字典(Dictionary)用法实例详解

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python dictionary,python中的dictionary,dictionary 字典,dictionary,cambridge dictionary等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了python中字典(Dictionary)用法。分享给大家供大家参考。具体分析如下:

字典(Dictionary)是一种映射结构的数据类型,由无序的“键-值对”组成。字典的键必须是不可改变的类型,如:字符串,数字,tuple;值可以为任何python数据类型。

1、新建字典

>>> dict1={} #建立一个空字典
>>> type(dict1)
<type 'dict'>

</div>

2、增加字典元素:两种方法

>>> dict1['a']=1 #第一种
>>> dict1
{'a': 1}
#第二种:setdefault方法
>>> dict1.setdefault('b',2)
2
>>> dict1
{'a': 1, 'b': 2}

</div>

3、删除字典

#删除指定键-值对
>>> dict1
{'a': 1, 'b': 2}
>>> del dict1['a'] #也可以用pop方法,dict1.pop('a')
>>> dict1
{'b': 2}
#清空字典
>>> dict1.clear()
>>> dict1 #字典变为空了
{}
#删除字典对象
>>> del dict1
>>> dict1
Traceback (most recent call last):
 File "<interactive input>", line 1, in <module>
NameError: name 'dict1' is not defined

</div>

4、字典的方法

1)get(key,default=None)

返回键值key对应的值;如果key没有在字典里,则返回default参数的值,默认为None

>>> dict1 #空的字典
{}
>>> dict1.get('a') #键‘a'在dict1中不存在,返回none
>>> dict1.get('d1','no1')  #default参数给出值'no1',所以返回'no1'
'no1'
>>> dict1['a']='no1' #插入一个新元素
>>> dict1
{'a': '1111'}
>>> dict1.get('a') #现在键'a'存在,返回其值
'1111'

</div>

2)clear 清空字典

3)has_key(key) 如果key出现在dict里则返回True;否则返回False

>>> dict1
{'a': '1111'}
>>> dict1.has_key('b')
False
>>> dict1.has_key('a')
True

</div>

4)items 返回dict的(键,值)tuple对的一个列表

>>> dict1
{'a': 'no1', 'b': '2222'}
>>> dict1.items()
[('a', 'no1'), ('b', '2222')]

</div>

5)keys 返回dict的键列表

6)values 返回dict的值列表

>>> dict1
{'a': 'no1', 'b': '2222'}
>>> dict1.keys()
['a', 'b']
>>> dict1.values()
['no1', '2222']

</div>

7)setdefault(key,default=None)

如果dict中有key,则返回key值,如果没有找到key,则在dict中加上该key,值由default参数给出,默认None

8)update(dict2)

把dict2的元素加入到dict中去,键字重复时会覆盖dict中的键值

>>> dict2
{'c': '3333', 'b': 'no2'}
>>> dict1 #dict2和dict1的键‘b'重复
{'a': 'no1', 'b': '2222'}
>>> dict1.update(dict2) #调用update后,dict1的键'b'值被覆盖了
>>> dict1
{'a': 'no1', 'c': '3333', 'b': 'no2'}

</div>

9)popitem 删除任意键-值对,并返回该键-值对,如字典为空,则产生异常

>>> dict1
{'b': 'no2'}
>>> dict1.popitem()
('b', 'no2')
>>> dict1
{}
>>> dict1.popitem()
Traceback (most recent call last):
 File "<interactive input>", line 1, in <module>
KeyError: 'popitem(): dictionary is empty'

</div>

10)pop(key,[d]) 删除指定键字的键-值对,并返回该键对应的值

>>> dict1
{'a': 'no1', 'c': '3333', 'b': 'no2'}
>>> dict1.pop('a')
'no1'
>>> dict1
{'c': '3333', 'b': 'no2'}

</div>

11)copy 返回字典的一个浅拷贝

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • 如何将python中的List转化成dictionary
  • python中字典(Dictionary)用法实例详解
  • python中字典(Dictionary)用法实例详解
  • Python中dictionary items()系列函数的用法实例
  • Python 字典(Dictionary)操作详解

相关文章

  • python 简单的多线程链接实现代码
  • python实现逻辑回归的方法示例
  • 最基础的Python的socket编程入门教程
  • 介绍Python中的一些高级编程技巧
  • Python中异常重试的解决方案详解
  • python统计日志ip访问数的方法
  • 用Python操作字符串之rindex()方法的使用
  • 使用Python装饰器在Django框架下去除冗余代码的教程
  • Python操作列表之List.insert()方法的使用
  • python下paramiko模块实现ssh连接登录Linux服务器

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python实现的文件夹清理程序分享
    • 浅谈五大Python Web框架
    • Python实现图像几何变换
    • Pycharm学习教程(6) Pycharm作为Vim编辑器使用
    • python的random模块及加权随机算法的python实现方法
    • Python的print用法示例
    • Python中函数参数设置及使用的学习笔记
    • Python中如何获取类属性的列表
    • Python实现冒泡,插入,选择排序简单实例
    • python常规方法实现数组的全排列

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有