Python基础7-字典

字典一种key - value 的数据类型。

字典是无序的,键必须是唯一的,但值则不必,值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组。

特性:

  1. 无顺序
  2. 去重
  3. 查询速度快,比列表快多
  4. 比list占用内存多(hash类型)

字典的常用操作

创建

直接创建

dict1 = {'student1':‘01‘,'color':['red','white'],'guangdong':{'guangzhou':['haizhu','tianhe'],'shenzhen':['nanshan','futian']}}

通过dict函数创建

#通过list创建
>>> student = [('student1','01'),('student2','02')]
>>> student_dict = dict(student)
>>> student_dict
{'student1': '01', 'student2': '02'}
#通过关键字参数创建
>>> student_dict = dict(student1='01',student2='02',student3='03')
>>> student_dict
{'student1': '01', 'student2': '02', 'student3': '03'}

  

追加

>>> student_dict
{'student1': '01', 'student2': '02', 'student3': '03'}
>>> student_dict['student4'] = '04'
>>> student_dict
{'student1': '01', 'student2': '02', 'student3': '03', 'student4': '04'}

 

修改

>>> student_dict
{'student1': '01', 'student2': '02', 'student3': '03', 'student4': '04'}
>>> student_dict['student4'] = '004'
>>> student_dict
{'student1': '01', 'student2': '02', 'student3': '03', 'student4': '004'}

 

删除

>>> student_dict
{'student1': '01', 'student2': '02', 'student3': '03', 'student4': '004'}
>>> student_dict.pop('student4')
'004'
>>> student_dict
{'student1': '01', 'student2': '02', 'student3': '03'}
>>> del student_dict['student3']
>>> student_dict
{'student1': '01', 'student2': '02'}
>>> student_dict.popitem()       #随机删除
('student2', '02')
>>> student_dict
{'student1': '01'}

 

查找

>>> student_dict
{'student1': '01', 'student2': '02', 'student3': '03', 'student4': '04'}
>>> 'student1' in student_dict
True
>>> student_dict.get('student1')
'01'
>>> student_dict['student1']
'01'
>>> student_dict.get('student5')
>>> student_dict['student5']      #如果一个key不存在,就报错,get方法不会,不存在只返回None

>>> student_dict
{'student1': '01', 'student2': '02', 'student3': '03', 'student4': '04'}
>>> student_dict.keys()
dict_keys(['student1', 'student2', 'student3', 'student4'])
>>> student_dict.values()
dict_values(['01', '02', '03', '04'])
>>> student_dict.items()
dict_items([('student1', '01'), ('student2', '02'), ('student3', '03'), ('student4', '04')])
>>> type(student_dict.items())
<class 'dict_items'>
>>> type(student_dict)
<class 'dict'>
#嵌套字典查找
>>> dict1 = {'student1':'01','color':['red','white'],'guangdong':{'guangzhou':['haizhu','tianhe'],'shenzhen':['nanshan','futian']}}
>>> dict1['guangdong']['guangzhou'][0]
'haizhu'

  

更新

>>> student_dict2 = {'student5':'05','student2':'002'}
>>> student_dict
{'student1': '01', 'student2': '02', 'student3': '03', 'student4': '04'}
>>> student_dict.update(student_dict2)   #用字典student_dict2更新,更新键对应的新值,添加新的键值对。
>>> student_dict
{'student1': '01', 'student2': '002', 'student3': '03', 'student4': '04', 'student5': '05'}

 

遍历

#方法1
for key in info:
    print(key,info[key])

#方法2
for k,v in info.items(): #会先把dict转成list,数据大时尽量不要用
    print(k,v)

  

字典相关的函数

 

序号函数及描述实例
1len(dict)
计算字典元素个数,即键的总数。
>>> dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'} >>> len(dict) 3
2str(dict)
输出字典,以可打印的字符串表示。
>>> dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'} >>> str(dict) "{'Name': 'Runoob', 'Class': 'First', 'Age': 7}"
3type(variable)
返回输入的变量类型,如果变量是字典就返回字典类型。
>>> dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'} >>> type(dict) <class 'dict'>

 

  

字典的内置方法 

 

序号函数及描述
1radiansdict.clear()
删除字典内所有元素
2radiansdict.copy()
返回一个字典的浅复制
3radiansdict.fromkeys()
创建一个新字典,以序列seq中元素做字典的键,val为字典所有键对应的初始值
4radiansdict.get(key, default=None)
返回指定键的值,如果值不在字典中返回default值
5key in dict
如果键在字典dict里返回true,否则返回false
6radiansdict.items()
以列表返回可遍历的(键, 值) 元组数组
7radiansdict.keys()
以列表返回一个字典所有的键
8radiansdict.setdefault(key, default=None)
和get()类似, 但如果键不存在于字典中,将会添加键并将值设为default
9radiansdict.update(dict2)
把字典dict2的键/值对更新到dict里
10radiansdict.values()
以列表返回字典中的所有值
11pop(key[,default])
删除字典给定键 key 所对应的值,返回值为被删除的值。key值必须给出。 否则,返回default值。
12popitem()
随机返回并删除字典中的一对键和值(一般删除末尾对)。

 

参考链接:http://www.runoob.com/python3/python3-dictionary.html

参考链接:http://www.cnblogs.com/alex3714/articles/5717620.html

 

转载于:https://www.cnblogs.com/chenqiyi/p/8464958.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值