标签:调用 other value date 初始 for循环 数组 实例 ble
Python基础--字典
字典的常用函数:
dict.clear( )--->无任何返回值
说明:
清除字典内的所有的元素
语法:
In [5]: dict.clear? Type: method_descriptor String form: <method ‘clear‘ of ‘dict‘ objects> Namespace: Python builtin Docstring: D.clear() -> None. Remove all items from D.
实例:
In [61]: dict_demo = {‘name‘:‘Kevin‘,‘age‘:18} In [62]: dict_demo.clear() In [63]: dict_demo Out[63]: {}
dict.copy()-->返回字典的浅复制
说明:
Python 字典 copy() 函数返回一个字典的浅复制。
语法:
In [22]: dict.copy? Type: builtin_function_or_method String form: <built-in method copy of dict object at 0x7f4d2c4577c8> Docstring: D.copy() -> a shallow copy of D
实例
In [14]: dict = {‘name‘:‘Kevin‘,‘age‘:18} In [15]: dict_copy = dict.copy() In [16]: dict_copy Out[16]: {‘name‘: ‘Kevin‘, ‘age‘: 18}
dict.fromkeys(seq[, value])----返回列表
说明:
Python 字典 fromkeys() 函数用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值。
参数:
语法:
In [25]: dict.fromkeys? Type: builtin_function_or_method String form: <built-in method fromkeys of type object at 0xa3cb40> Definition: dict.fromkeys(type, iterable, value) Docstring: Returns a new dict with keys from iterable and values equal to value.
实例:
In [33]: dict_demo = {} In [34]: l = [‘name‘,‘age‘,‘sex‘] In [35]: dict_demo = {} In [36]: dict_demo = dict_demo.fromkeys(l)
In [37]: dict_demo Out[37]: {‘sex‘: None, ‘name‘: None, ‘age‘: None} In [38]: dict_demo = dict_demo.fromkeys(l,‘xx‘) In [39]: dict_demo Out[39]: {‘sex‘: ‘xx‘, ‘name‘: ‘xx‘, ‘age‘: ‘xx‘}
dict.get(key,default=None)-------返回字典指定键对应的值,如果值不存在则返回默认值。
说明:
Python 字典 get() 函数返回指定键的值,如果值不在字典中返回默认值。
语法:
In [2]: dict.get? Type: method_descriptor String form: <method ‘get‘ of ‘dict‘ objects> Namespace: Python builtin Docstring: D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.
实例:
In [23]: dict Out[23]: {‘age‘: 18, ‘sex‘: ‘男‘, ‘name‘: ‘Bob‘} In [24]: dict.get(‘name‘,‘please try again‘) Out[24]: ‘Bob‘ In [25]: dict.get(‘school‘,‘please try again‘) Out[25]: ‘please try again‘
dict.items()--->返回可遍历的(键, 值) 元组数组。
说明:
Python 字典 items() 方法以列表返回可遍历的(键, 值) 元组数组。
语法:
In [41]: dict.items? Type: builtin_function_or_method String form: <built-in method items of dict object at 0x7f4d2c357cc8> Docstring: D.items() -> a set-like object providing a view on D‘s items
实例:
In [42]: dict_demo = {‘name‘:‘kevin‘,‘age‘:18} In [43]: dict_demo.items() Out[43]: dict_items([(‘name‘, ‘kevin‘), (‘age‘, 18)]) In [44]: type(dict_demo.items()) Out[44]: dict_items
字典的for循环:
In [50]: dict_demo Out[50]: {‘name‘: ‘kevin‘, ‘age‘: 18} In [51]: for k,v in dict_demo.items(): ....: print(k,v) ....: name kevin age 18
dict.keys()--->返回的类型为列表-列表中包含字典所有的键。
说明:
Python 字典 keys() 方法以列表返回一个字典所有的键。
语法:
In [57]: dict.keys? Type: builtin_function_or_method String form: <built-in method keys of dict object at 0x7f4d2c357cc8> Docstring: D.keys() -> a set-like object providing a view on D‘s keys
实例:
In [52]: dict_demo Out[52]: {‘name‘: ‘kevin‘, ‘age‘: 18} In [53]: dict_demo.keys() Out[53]: dict_keys([‘name‘, ‘age‘]) In [54]: dict_list = dict_demo.keys() In [55]: dict_list Out[55]: dict_keys([‘name‘, ‘age‘]) In [56]: for i in dict_list: ....: print(i) ....: name age
pop(key[,default])---返回值为被删除的值
说明:
Python 字典 pop() 方法删除字典给定键 key 所对应的值,返回值为被删除的值。key值必须给出。 否则,返回default值。
参数:
语法:
In [71]: dict.pop? Type: builtin_function_or_method String form: <built-in method pop of dict object at 0x7f4d2c357cc8> Docstring: D.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised
实例:
In [67]: dict_demo = {‘name‘:‘kevin‘,‘age‘:18,‘sex‘:‘man‘} In [68]: dict_demo.pop(‘name‘) Out[68]: ‘kevin‘ In [69]: dict_demo.pop(‘name‘,‘Sorry please try again‘) Out[69]: ‘Sorry please try again‘ In [70]: dict_demo Out[70]: {‘sex‘: ‘man‘, ‘age‘: 18}
dict.setdefault(key,default=None)----如果 key 在 字典中,返回对应的值。如果不在字典中,则插入 key 及设置的默认值 default,并返回 default ,default 默认值为 None。
说明:
Python 字典 setdefault() 方法和get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值。
参数:
key: 查找的键
default : 键不存在时,设置默认的键值
实例:
In [1]: dict = {‘name‘:‘Kevin‘,‘age‘:18,‘sex‘:‘男‘} In [2]: dict.setdefault(‘name‘) Out[2]: ‘Kevin‘ In [3]: dict.setdefault(‘school‘,‘sorry please try agein‘) Out[3]: ‘sorry please try agein‘
dict.update(dict2)---返回一个新的字典
说明:
Python 字典 update() 函数把字典参数 dict2 的 key/value(键/值) 对更新到字典 dict 里。
参数:
dict2 -- 添加到指定字典dict里的字典。
实例
In [4]: dict = {‘name‘:‘Kevin‘,‘age‘:18,‘sex‘:‘男‘} In [5]: dict2 = {‘school‘:‘heigh school‘} In [6]: dict.update(dict2) In [7]: dict Out[7]: {‘school‘: ‘heigh school‘, ‘age‘: 18, ‘sex‘: ‘男‘, ‘name‘: ‘Kevin‘}
注意:当插入的字典与初始字典具有相同的键的时候那么值将会被覆盖
In [8]: dict Out[8]: {‘school‘: ‘heigh school‘, ‘age‘: 18, ‘sex‘: ‘男‘, ‘name‘: ‘Kevin‘} In [9]: dict3 = {‘name‘:‘Bob‘} In [10]: dict.update(dict3) In [11]: dict Out[11]: {‘school‘: ‘heigh school‘, ‘age‘: 18, ‘sex‘: ‘男‘, ‘name‘: ‘Bob‘}
dict.values()--以列表的形式返回字典中的所有的值
说明:
Python 字典 values() 方法以列表返回字典中的所有值。
实例:
In [11]: dict Out[11]: {‘school‘: ‘heigh school‘, ‘age‘: 18, ‘sex‘: ‘男‘, ‘name‘: ‘Bob‘} In [12]: dict.values() Out[12]: dict_values([‘heigh school‘, 18, ‘男‘, ‘Bob‘]) In [13]: list(dict.values()) Out[13]: [‘heigh school‘, 18, ‘男‘, ‘Bob‘]
dict.popitem() ----返回值是已经删除的字典的键值对
说明:
Python 字典 popitem() 方法随机返回并删除字典中的一对键和值(一般删除末尾对)。
如果字典已经为空,却调用了此方法,就报出KeyError异常。
实例:
In [14]: dict Out[14]: {‘school‘: ‘heigh school‘, ‘age‘: 18, ‘sex‘: ‘男‘, ‘name‘: ‘Bob‘} In [15]: dict.popitem() Out[15]: (‘school‘, ‘heigh school‘) In [16]: dict Out[16]: {‘age‘: 18, ‘sex‘: ‘男‘, ‘name‘: ‘Bob‘}
字典的操作
待更新。。。。
标签:调用 other value date 初始 for循环 数组 实例 ble
原文地址:https://www.cnblogs.com/Echo-O/p/9323297.html