定义一个叫做name的字典 >>> name = { ... 1860:{ ... "name":"wangfei", ... "age":18, ... "addr":"hangzhou" ... }, ... 1861:{ ... "name": "lisi", ... "age": 19, ... "addr": "chuzhou" ... }, ... 1862: { ... "name": "wanger", ... "age": 20, ... "addr": "hefei" ... }, ... 1863: { ... "name": "wangerma", ... "age": 23, ... "addr": "beijing" ... }, ... } >>> print (name) {1860: {‘age‘: 18, ‘name‘: ‘wangfei‘, ‘addr‘: ‘hangzhou‘}, 1861: {‘age‘: 19, ‘name‘: ‘lisi‘, ‘addr‘: ‘chuzhou‘}, 1862: {‘age‘: 20, ‘name‘: ‘wanger‘, ‘addr‘: ‘hefei‘}, 1863: {‘age‘: 23, ‘name‘: ‘wangerma‘, ‘addr‘: ‘beijing‘}} >>> 我想从字典name里取出id为1861的name的值 >>> print (name[1861]["name"]) lisi 修改id为1861的name对应的值为wangerma >>> print (name[1861]["name"]) wangerma 在id为1861的小字典里添加一个新的信息,mobile 注意:字典是无序的 >>> name[1861]["mobile"] = 18696511327 >>> print (name[1861]) {‘age‘: 19, ‘mobile‘: 18696511327, ‘name‘: ‘wangerma‘, ‘addr‘: ‘chuzhou‘} >>> 删除字典中的元素(有2中方法,.pop 和del, 建议使用del来删除。) del方式(推荐使用) >>> del name[1861]["mobile"] >>> print (name[1861]) {‘age‘: 19, ‘name‘: ‘wangerma‘, ‘addr‘: ‘chuzhou‘} pop方式(不推荐使用) >>> name[1861].pop("addr") ‘chuzhou‘ >>> name[1861] {‘age‘: 19, ‘name‘: ‘wangerma‘} 获取字典中某个值 2种方式 get方式获取 >>> name.get(1861) {‘age‘: 19, ‘name‘: ‘wangerma‘} 下标方式获取 >>> name[1861] {‘age‘: 19, ‘name‘: ‘wangerma‘} update语句,将新字典中的值更新到旧的字典中,如果旧的字典中有就覆盖,没有就添加。 现在定义一个新的字典 >>> dic2 = { ... "name":"hellow", ... 1862: { ... "name": "wanger", ... "age": 20, ... "addr": "hefei" ... }, ... } >>> name.update(dic2) 发现新的数据被添加,原先存在的数据被覆盖 >>> name {1860: {‘age‘: 18, ‘name‘: ‘wangfei‘, ‘addr‘: ‘hangzhou‘}, 1861: {‘age‘: 19, ‘name‘: ‘wangerma‘}, 1862: {‘age‘: 20, ‘name‘: ‘wanger‘, ‘addr‘: ‘hefei‘}, 1863: {‘age‘: 23, ‘name‘: ‘wangerma‘, ‘addr‘: ‘beijing‘}, ‘name‘: ‘hellow‘} items 将字典中的数据转换为列表 注意:在字典的数据量比较大的时候不要这么做,比较耗时。 >>> name.items() dict_items([(1860, {‘age‘: 18, ‘name‘: ‘wangfei‘, ‘addr‘: ‘hangzhou‘}), (1861, {‘age‘: 19, ‘name‘: ‘wangerma‘}), (1862, {‘age‘: 20, ‘name‘: ‘wanger‘, ‘addr‘: ‘hefei‘}), (1863, {‘age‘: 23, ‘name‘: ‘wangerma‘, ‘addr‘: ‘beijing‘}), (‘name‘, ‘hellow‘)]) values 打印字典所有的值 >>> name.values() dict_values([{‘age‘: 18, ‘name‘: ‘wangfei‘, ‘addr‘: ‘hangzhou‘}, {‘age‘: 19, ‘name‘: ‘wangerma‘}, {‘age‘: 20, ‘name‘: ‘wanger‘, ‘addr‘: ‘hefei‘}, {‘age‘: 23, ‘name‘: ‘wangerma‘, ‘addr‘: ‘beijing‘}, ‘hellow‘]) keys 打印字典所有的key >>> name.keys() dict_keys([1860, 1861, 1862, 1863, ‘name‘]) 判断字典中的key在字典中是否存在 >>> 1861 in name True setdefault 判断字典中的值是否存在,如果不存在就返回默认的值none >>> name.setdefault(1861,"none") {‘age‘: 19, ‘name‘: ‘wangerma‘} >>> name.setdefault(1869,"none") ‘none‘ >>> 将列表中的元素取出来,作为key,值为values(请不要用,了解即可) >>> dict.fromkeys([1,2,3,4,5],"hehe") {1: ‘hehe‘, 2: ‘hehe‘, 3: ‘hehe‘, 4: ‘hehe‘, 5: ‘hehe‘} 遍历字典,有2种方法。建议是方法二,第一种方法效率低。 方法一 >>> for key,values in name.items(): ... print (key, values) ... 1860 {‘age‘: 18, ‘name‘: ‘wangfei‘, ‘addr‘: ‘hangzhou‘} 1861 {‘age‘: 19, ‘name‘: ‘wangerma‘} 1862 {‘age‘: 20, ‘name‘: ‘wanger‘, ‘addr‘: ‘hefei‘} 1863 {‘age‘: 23, ‘name‘: ‘wangerma‘, ‘addr‘: ‘beijing‘} 1878788 none name hellow 1869 none 1878 ok >>> 方法二 >>> for key in name: ... print (key, name[key]) ... 1860 {‘age‘: 18, ‘name‘: ‘wangfei‘, ‘addr‘: ‘hangzhou‘} 1861 {‘age‘: 19, ‘name‘: ‘wangerma‘} 1862 {‘age‘: 20, ‘name‘: ‘wanger‘, ‘addr‘: ‘hefei‘} 1863 {‘age‘: 23, ‘name‘: ‘wangerma‘, ‘addr‘: ‘beijing‘} 1878788 none name hellow 1869 none 1878 ok >>>
时间: 2024-10-14 12:46:53