a = {'name':'xiaoge','age':18,'face':'handsome'}
b = a.pop('name')
c = a.pop('like','yang') #这样是对的
d = a.pop('age',15) #如果没有age键,则返回设置的默认值15,如果有,返回age对应的值
print(a,b,c,d)
#{'age': 18, 'face': 'handsome'} xiaoge yang 18
#下面的d是错的,因为没有键like,而且没给like设置默认值
d = a.pop('like')
#KeyError: 'like'
items
items()方法把字典中每对key和value组成一个元组,并把这些元组放在列表中返回
返回可遍历的(键, 值) 元组数组
a ={'name':'xiaoge','age':18,'hobby':'girl'}for key,value in a.items():print('key=',key,'value=',value)fortuplein a.items():print(tuple)print(dict[tuple])#key= name value= xiaoge
key= age value=18
key= hobby value= girl
('name','xiaoge'){'name':'xiaoge'}('hobby','girl'){'hobby':'girl'}('age',18){'age':18}
两个列表组成字典/两个元素的元组转换成字典
data ={}
a =[1,34,3,6]
b =['haha','xixi','hoho','heihei']
data.update(zip(a, b))print(dict(zip(a,b)))print(data)#{1: 'haha', 34: 'xixi', 3: 'hoho', 6: 'heihei'}{1:'haha',34:'xixi',3:'hoho',6:'heihei'}