首页 > 代码库 > Python字典

Python字典

Python的字典是一种Key-value的数据类型,和js的数据格式一样。

字典的定义

name = {
‘name‘:‘zhangshan‘,
‘age‘:23,
‘address‘:‘beijing‘,
54:‘asd‘,
‘address‘:‘hebei‘
}
print(name)#{‘name‘: ‘zhangshan‘, 54: ‘asd‘, ‘address‘: ‘hebei‘, ‘age‘: 23

1、打印的结果和我定义字典的顺序不一样,说明字典是无序的。

2、定义了两个‘address‘ 最后定义的把之前的覆盖了 说明字典的key是唯一的。

字典的添加

dic[key] = value

name = {
    ‘name‘:‘zhangshan‘,
    ‘age‘:23,
    ‘address‘:‘beijing‘,
    54:‘asd‘,
    ‘address‘:‘hebei‘
}
name[‘phone‘]=‘789456‘
print(name)#{‘phone‘: ‘789456‘, ‘age‘: 23, ‘name‘: ‘zhangshan‘, ‘address‘: ‘hebei‘, 54: ‘asd‘}

 字典的删除

pop(key) 删除指定的key

name = {
    ‘name‘:‘zhangshan‘,
    ‘age‘:23,
    ‘address‘:‘beijing‘,
}
name.pop(‘age‘)
print(name)#{‘name‘: ‘zhangshan‘, ‘address‘: ‘beijing‘}

 字典的修改

dic[key] = new_value

name = {
    ‘name‘:‘zhangshan‘,
    ‘age‘:23,
    ‘address‘:‘beijing‘,
}
name[‘name‘] = ‘lisi‘
print(name)#{‘age‘: 23, ‘name‘: ‘lisi‘, ‘address‘: ‘beijing‘}

 字典的查找

1 dic[key]

name = {
    ‘name‘:‘zhangshan‘,
    ‘age‘:23,
    ‘address‘:‘beijing‘,
}
print(name[‘name‘])#zhangshan

 这种方法不是很好,如果查一个不存在的key会报错。

name = {
    ‘name‘:‘zhangshan‘,
    ‘age‘:23,
    ‘address‘:‘beijing‘,
}
print(name[‘idcode‘])

 技术分享

2 get(key) 如果有了就返回 没有了就是none

name = {
    ‘name‘:‘zhangshan‘,
    ‘age‘:23,
    ‘address‘:‘beijing‘,
}
print(name.get(‘idcode‘))#None

 3 in 判断这个key是不是在字典中 结果是布尔值

name = {
    ‘name‘:‘zhangshan‘,
    ‘age‘:23,
    ‘address‘:‘beijing‘,
}
print(‘idcode‘ in name)#False
print(‘address‘ in name)#True

 

其它操作

values() 打印所有的value

name = {
    ‘name‘:‘zhangshan‘,
    ‘age‘:23,
    ‘address‘:‘beijing‘,
}
print(name.values())#dict_values([23, ‘zhangshan‘, ‘beijing‘])

 

keys() 打印所有的key

name = {
    ‘name‘:‘zhangshan‘,
    ‘age‘:23,
    ‘address‘:‘beijing‘,
}
print(name.keys())#dict_keys([‘age‘, ‘address‘, ‘name‘])

 setdefault(key,value)  先查字典,如果能查到有这个key就返回,没有就创建一个新的key,赋值value。

name = {
    ‘name‘:‘zhangshan‘,
    ‘age‘:23,
    ‘address‘:‘beijing‘,
}
name.setdefault(‘phone‘,‘789456‘)
name.setdefault(‘age‘,25)
print(name)#{‘phone‘: ‘789456‘, ‘age‘: 23, ‘name‘: ‘zhangshan‘, ‘address‘: ‘beijing‘}

update(dic) 两个字典合并,相同的元素覆盖。

name = {
    ‘name‘:‘zhangshan‘,
    ‘age‘:23,
    ‘address‘:‘beijing‘,
}
name2 = {
    ‘name‘:‘lisi‘,
    ‘phone‘:‘456456‘,
    ‘car‘:‘SKODA‘
}
name.update(name2)
print(name)#{‘age‘: 23, ‘phone‘: ‘456456‘, ‘name‘: ‘lisi‘, ‘car‘: ‘SKODA‘, ‘address‘: ‘beijing‘}

 items() 字典转列表,每一组key-value 转成元组,key作为元组的第一个值,value作为元组的第二个值。

name2 = {
    ‘name‘:‘lisi‘,
    ‘phone‘:‘456456‘,
    ‘car‘:‘SKODA‘
}

print(name2.items())#dict_items([(‘phone‘, ‘456456‘), (‘car‘, ‘SKODA‘), (‘name‘, ‘lisi‘)])

 如果是字典嵌套

name2 = {
    ‘name‘:‘lisi‘,
    ‘phone‘:‘456456‘,
    ‘car‘:‘SKODA‘,
    ‘child‘:{
        ‘key_1‘:1
    }
}

print(name2.items())#dict_items([(‘car‘, ‘SKODA‘), (‘child‘, {‘key_1‘: 1}), (‘name‘, ‘lisi‘), (‘phone‘, ‘456456‘)])

可以看到, 嵌套的字典并没有转成元组。

循环

1、简单的for循环

name = {
    ‘name‘:‘zhangshan‘,
    ‘age‘:23,
    ‘address‘:‘beijing‘,
}

for i in name:
    print(i,name[i])
#####################
name zhangshan
age 23
address beijing

2、items() 转列表循环

for key,value in name.items():
    print(key,value )

#######################
address beijing
name zhangshan
age 23

 如果数据量很大的话,转列表的操作会很低效。

 

Python字典