字典的操作(增删改查)
Posted startl
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字典的操作(增删改查)相关的知识,希望对你有一定的参考价值。
字典的操作(增删改查)
#_author:star
#date:2019/10/26
# 字典
# 不可变类型:整形,字符串,元组
# 可变类型:字典,列表
#字典两个特点:无序,键唯一
a=10
print(id(a))#1351629200
b=a
print(id(b))#1351629200
b=15
print(id(b))#1351629280
print(a,b)# 10 15
#字典的创建方式一
dic={‘name‘:‘star‘,2:‘girl‘,‘age‘:22,‘hobby‘:{‘girl_name‘:‘angle‘,‘age‘:22,‘sex‘:‘female‘},‘sex‘:‘male‘}
print(dic)# {‘name‘: ‘star‘, 2: ‘girl‘, ‘age‘: 22, ‘hobby‘: {‘girl_name‘: ‘angle‘, ‘age‘: 22, ‘sex‘: ‘female‘}, ‘sex‘: ‘male‘}
print(dic[‘hobby‘])#{‘girl_name‘: ‘angle‘, ‘age‘: 22, ‘sex‘: ‘female‘}
#字典的创建方式二
c=dict([("name","star"),])
print(c)#{‘name‘: ‘star‘}
d=dict(((‘age‘,36),))#注意,如果内部全部是(),记得加逗号
print(d)#{‘age‘: 36}
#字典的操作:1.增
dic={‘name‘:‘tong‘,‘age‘:22}
dic[‘hobby‘]=‘girl‘
print(dic)#{‘name‘: ‘tong‘, ‘age‘: 22, ‘hobby‘: ‘girl‘}
dic.setdefault(‘age‘,23)
tr=dic.setdefault(‘age‘,23)#setdefault是有返回值的,返回值是(键存在,不改动,返回字典中相应的键对应的值)
print(tr)#22
print(dic)# {‘name‘: ‘tong‘, ‘age‘: 22, ‘hobby‘: ‘girl‘}
dic.setdefault(‘interest‘,‘coding‘)
tr1=dic.setdefault(‘interest‘,‘coding‘)#键不存在,在字典中增加新的键值对,并返回相应的值
print(tr1)#coding
print(dic)# {‘name‘: ‘tong‘, ‘age‘: 22, ‘hobby‘: ‘girl‘, ‘interest‘: ‘coding‘}
#2.查---通过键去查找
dic2={‘name‘:‘star‘,2:‘girl‘,‘age‘:22,‘hobby‘:{‘girl_name‘:‘angle‘,‘age‘:22,‘sex‘:‘female‘},‘sex‘:‘male‘}
print(dic2[‘age‘])#22
print(dic2.keys())#dict_keys([‘name‘, 2, ‘age‘, ‘hobby‘, ‘sex‘])
print(type(dic2.keys()))#<class ‘dict_keys‘>
#想要拿到一个列表,转换
print(list(dic2.keys()))#[‘name‘, 2, ‘age‘, ‘hobby‘, ‘sex‘]
print(dic2.values())#dict_values([‘star‘, ‘girl‘, 22, {‘girl_name‘: ‘angle‘, ‘age‘: 22, ‘sex‘: ‘female‘}, ‘male‘])
print(list(dic2.values()))#[‘star‘, ‘girl‘, 22, {‘girl_name‘: ‘angle‘, ‘age‘: 22, ‘sex‘: ‘female‘}, ‘male‘]
print(dic2.items())#dict_items([(‘name‘, ‘star‘), (2, ‘girl‘), (‘age‘, 22), (‘hobby‘, {‘girl_name‘: ‘angle‘, ‘age‘: 22, ‘sex‘: ‘female‘}), (‘sex‘, ‘male‘)])
print(list(dic2.items()))#[(‘name‘, ‘star‘), (2, ‘girl‘), (‘age‘, 22), (‘hobby‘, {‘girl_name‘: ‘angle‘, ‘age‘: 22, ‘sex‘: ‘female‘}), (‘sex‘, ‘male‘)]
#3.修改
dic3={‘name‘:‘star‘,2:‘girl‘,‘age‘:22,‘sex‘:‘male‘}
dic3[‘name‘]=‘xing‘
print(dic3)#{‘name‘: ‘xing‘, 2: ‘girl‘, ‘age‘: 22, ‘sex‘: ‘male‘}
dic4={1:‘111‘,‘lover‘:‘cake‘}
dic3.update(dic4)
print(dic3)#{‘name‘: ‘xing‘, 2: ‘girl‘, ‘age‘: 22, ‘sex‘: ‘male‘, 1: ‘111‘, ‘lover‘: ‘cake‘}
dic5={‘name‘:‘lai‘}#将原来的值替换
dic3.update(dic5)
print(dic3)#{‘name‘: ‘lai‘, 2: ‘girl‘, ‘age‘: 22, ‘sex‘: ‘male‘, 1: ‘111‘, ‘lover‘: ‘cake‘}
#4.删
dic6={‘name‘:‘star‘,2:‘girl‘,‘age‘:22,‘sex‘:‘male‘}
del dic6[‘name‘]#删除字典中指定键值对
print(dic6)#{2: ‘girl‘, ‘age‘: 22, ‘sex‘: ‘male‘}
re=dic6.pop(‘sex‘)#删除字典中指定键值对,并返回该键对应的值
print(re)
print(dic6)#{2: ‘girl‘, ‘age‘: 22}
dic6.clear()
print(dic6)#{}
dic7={‘name1‘:‘star1‘,21:‘girl1‘,‘age1‘:221,‘sex1‘:‘male1‘}
re1=dic7.popitem() #随机删除某组键值对
print(re1)#(‘sex1‘, ‘male1‘) 并以元组方式返回值
print(dic7)#{‘name1‘: ‘star1‘, 21: ‘girl1‘, ‘age1‘: 221}
del dic7#删除整个字典
#print(dic7)因为字典已经不存在,所以输出报错
#5.其他操作以及涉及到的方法
#(5-----1)创建字典的第三种方式
dic8=dic.fromkeys([‘so1‘,‘so2‘,‘so3‘],‘star‘)
print(dic8)#{‘so1‘: ‘star‘, ‘so2‘: ‘star‘, ‘so3‘: ‘star‘}
dic8[‘so2‘]=‘ooo‘
print(dic8)#{‘so1‘: ‘star‘, ‘so2‘: ‘ooo‘, ‘so3‘: ‘star‘}
dic9=dic.fromkeys([‘so1‘,‘so2‘,‘so3‘],[‘star1‘,‘star2‘])
print(dic9)#{‘so1‘: [‘star1‘, ‘star2‘], ‘so2‘: [‘star1‘, ‘star2‘], ‘so3‘: [‘star1‘, ‘star2‘]}
dic9[‘so2‘][1]=‘star3‘
print(dic9)#{‘so1‘: [‘star1‘, ‘star3‘], ‘so2‘: [‘star1‘, ‘star3‘], ‘so3‘: [‘star1‘, ‘star3‘]}
dic10={‘name‘:‘star‘,2:‘girl‘,‘age‘:22,‘sex‘:‘male‘}
#(copy()
r=dic10.copy()
print(r)#{‘name‘: ‘star‘, 2: ‘girl‘, ‘age‘: 22, ‘sex‘: ‘male‘}
print(dic10)#{‘name‘: ‘star‘, 2: ‘girl‘, ‘age‘: 22, ‘sex‘: ‘male‘}
#(5----2)字典嵌套
inf={
‘asia‘:{‘japon‘:‘height_produce‘,‘chinese‘:‘hand_produce‘,‘singrapo‘:[‘jenry‘,‘port‘]},
‘america‘:{‘England‘:‘car‘,‘US‘:‘big_car‘,‘gard_man‘:[‘handsome‘,‘kindness‘,‘great‘]},
}
print(inf[‘asia‘][‘singrapo‘][1])#port
print(inf[‘america‘][‘gard_man‘][2])#great
#(5---3)字典排序
s1={2:‘222‘,6:‘666‘,0:‘000‘}
print(sorted(s1))#[0, 2, 6]
print(sorted(s1.values()))#[‘000‘, ‘222‘, ‘666‘]
print(sorted(s1.items()))#[(0, ‘000‘), (2, ‘222‘), (6, ‘666‘)]
#( 5--4) 字典的遍历
# for j in s2:
# print(j)#按照键打印name
# age
s2={‘name‘:‘star‘,‘age‘:22}
for i in s2:
print(i,s2.get(i))
# name age
# star 22
s3 = {‘name‘: ‘star‘, ‘age‘: 22}
for i in s3:
print(i, s3[i])
# name age
# star 22
for i,v in enumerate(s3.items(),1):
print(i,v)
# 1 (‘name‘, ‘star‘)
# 2 (‘age‘, 22)
for i,v in s3.items():
print(i,v)
# name star
# age 22
以上是关于字典的操作(增删改查)的主要内容,如果未能解决你的问题,请参考以下文章