1.通过key获取value

  dict = {key1: value1, key2:value2}

  dict[‘key1’] 可获取到key1对应的value1  

person = {'name': 'tt', 'age': 13}
print(person['age'])  # 13

test_dict = {'name': 'll', 'age': 90}
print(test_dict['height'])  # 无value时程序报错,KeyError: 'height'

  通过dict[key] = value ,当key值存在是,会修改原value、当key值不存在时,会将key:value键值对添加到字典中;

person = {'name': 'tt', 'age': 13}
person['age'] = 24
print(person)  # {'name': 'tt', 'age': 24}
person['height'] = 178
print(person)  # {'name': 'tt', 'age': 24, 'height': 178}

 

2.字典的update()方法

  dict.update(new_dict) 一次添加多个键值对,同样key值存在时更新key对应的value、key值不存在是添加key:value对;

person = {'name': 'tt', 'age': 13}
person.update({'like': 'football', 'age': 134})
print(person)  # {'name': 'tt', 'age': 134, 'like': 'football'}

 

3.字典的setdefault()方法

  dict.setdefault(key, default_value)  

  key值存在时,直接返回对应的value;key值不存在时,将key: default_value键值对添加进字典;

  default_value值可省略,默认是None;

person = {'name': 'tt', 'age': 13}
print(person.setdefault('age', 45))  # 13
print(person.setdefault('like', 'football'))  # football
print(person)  # {'name': 'tt', 'age': 13, 'like': 'football'}
print(person.setdefault('height'))  # None
print(person)  # {'name': 'tt', 'age': 13, 'like': 'football', 'height': None}

 

4.获取字典全部值

  dict.keys()  获取字典中全部key值,返回一个伪列表;

  dict.values() 获取字典中全部value的值,返回一个伪列表;

  dict.items() 获取字典所有的键值对, 返回伪列表,各键值对存在列表内的元组中;

person = {'name': 'tt', 'age': 13}
print(person.keys())  # dict_keys(['name', 'age'])
print(list(person.keys()))  # ['name', 'age'] (可以转化成列表)

print(person.values())  # dict_values(['tt', 13])
print(list(person.values()))  # ['tt', 13]

print(person.items())  # dict_items([('name', 'tt'), ('age', 13)])
print(list(person.items()))  # [('name', 'tt'), ('age', 13)]

 

5.字典的get() 方法

  dict.get(key)  获取对应value,无key时、默认返回None;

  dict.get(key,  default_value)  无key时,可以指定返回结果default_value;

person = {'name': 'tt', 'age': 13}
print(person.get('age'))  # 13
print(person.get('like'))  # None
print(person.get('like', 'no key'))  # no key

print(person['like'])  # 程序报错,KeyError: 'like'
# person['like']方式比get()方式可以节省部分时间,不用做key是否存在的判断(但基本可忽略不计)
# get()方式比person['like']方式,在返回结果上更友善些

 

6.字典的删除操作

  dict.pop(key)  删除key:value对,并返回value值;key不存在时会报错;

  dict.clear()  清空字典;

  dict.popitem()  删除字典的最后一个键值对, 返回键值对存储在元组中;

  同样可借助python内置函数del;

person = {'name': 'tt', 'age': 13, 'like': 'football', 'height': 168}
print(person.pop('name'))  # tt (操作有返回值,值是value)
print(person)  # {'age': 13, 'like': 'football', 'height': 168}

print(person.popitem())  # ('height', 168)
print(person)  # {'age': 13, 'like': 'football'}

del person['like']
print(person)  # {'age': 13}
# del person['name']  # KeyError: 'name' (无对应key时,会报错)

person.clear()
print(person)  # {}

 

7.字典的copy()函数

  复制字典内元素生成新字典;

# dict.copy() 依旧属于浅拷贝
person = {
    'xiaoming': {
        'age': 23,
        'height': 178
    },
    'xiaohong': {
        'age': 26,
        'height': 168
    }
}
new_person = person.copy()
new_person['xiaoming']['age'] = 999
print(new_person)  # {'xiaoming': {'age': 999, 'height': 178}, 'xiaohong': {'age': 26, 'height': 168}}
print(person)  # {'xiaoming': {'age': 999, 'height': 178}, 'xiaohong': {'age': 26, 'height': 168}}
# import copy, copy.deepcopy() 可完成深拷贝操作
import copy
person = {
    'xiaoming': {
        'age': 23,
        'height': 178
    },
    'xiaohong': {
        'age': 26,
        'height': 168
    }
}
new_person = copy.deepcopy(person)
new_person['xiaoming']['age'] = 999
print(new_person)  # {'xiaoming': {'age': 999, 'height': 178}, 'xiaohong': {'age': 26, 'height': 168}}
print(person)  # {'xiaoming': {'age': 23, 'height': 178}, 'xiaohong': {'age': 26, 'height': 168}}

 

8.其它简单操作

  in ,not in 成员判断;

  len(dict) 判断字典长度;

test_dict = {'name': 'll', 'age': 90}
print('name' in test_dict)  # True
print(len(test_dict))  # 2

# 字典没有累加累乘操作

  

总结

    

原文地址:http://www.cnblogs.com/white-list/p/16807777.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性