Python的字典及内置函数
2018-03-05 22:35
399 查看
python的字典是一系列键值对。{}
ex info={'mother':'meat','father':'apple','brother':'fish','me':'water'}
访问指定键元素: print(info['mother'])
添加键值:info['aunt']='banana'
通常先建立一个空字典,在添加键值对 info{}
修改值:info['me']='wahaha'
删除键值: del info['me'] (KeyError )(访问不存在的键值。例如删除轴在访问 info['me'])
遍历字典:
遍历所有的键值:
for k,v in info.items():
print('key='+k)
print('value='+v)
print(info.items()):
dict_items([('mother', 'meat'), ('father', 'apple'), ('brother', 'fish'), ('me', 'wahaha')])
遍历所有的健:
for k in info.keys():
print('key='+k.title())
遍历所有的值
for v in info.values():
print('value='+v.title())
按顺序遍历所有的健:
for k in sorted(info.keys()):
print(k.title())
len(info) 返回字典的键值对数
info={'mother':50,'father':52,'brother':17,'me':23}
ss=(str(info)) 把字典转化为字符串形式
例如 print(ss[0:6]) 输出“{moth"
info.clear() 删除所有的键值对,info=[]
a=info.copy() a copy info所有的键值对
type(info ) 返回<class 'dict'>
info={'mother':50,'father':52,'brother':17,'me':23}
print('uncle' in info) 返回FALSE
print('me' in info) 返回True
info.get('me'): 查看‘me’的值是多少,不存在返回NONE
print(info.get('me')) 返回23
print(info.get('uncle')) 返回None
info={'mother':50,'father':52,'brother':17,'me':23}
info.setdefault('cousin',59) 类似info.get() 但是 当cousin不存在时生产新的键值对
>>info={'mother':50,'father':52,'brother':17,'me':23,'cousin':'59'}
but info.setdefault('mother',59)
>>>>info={'mother':50,'father':52,'brother':17,'me':23'}
info.pop('me')
info={'mother':50,'father':52,'brother':17,'me':23}
a=info.pop('me') >>a=23
print(info) info>>info={'mother':50,'father':52,'brother':17}
info={'mother':50,'father':52,'brother':17,'me':23}
a=info.pop('cousin')
print(a)
print(info) 报错,返回 KeyError
ex info={'mother':'meat','father':'apple','brother':'fish','me':'water'}
访问指定键元素: print(info['mother'])
添加键值:info['aunt']='banana'
通常先建立一个空字典,在添加键值对 info{}
修改值:info['me']='wahaha'
删除键值: del info['me'] (KeyError )(访问不存在的键值。例如删除轴在访问 info['me'])
遍历字典:
遍历所有的键值:
for k,v in info.items():
print('key='+k)
print('value='+v)
print(info.items()):
dict_items([('mother', 'meat'), ('father', 'apple'), ('brother', 'fish'), ('me', 'wahaha')])
遍历所有的健:
for k in info.keys():
print('key='+k.title())
遍历所有的值
for v in info.values():
print('value='+v.title())
按顺序遍历所有的健:
for k in sorted(info.keys()):
print(k.title())
len(info) 返回字典的键值对数
info={'mother':50,'father':52,'brother':17,'me':23}
ss=(str(info)) 把字典转化为字符串形式
例如 print(ss[0:6]) 输出“{moth"
info.clear() 删除所有的键值对,info=[]
a=info.copy() a copy info所有的键值对
type(info ) 返回<class 'dict'>
info={'mother':50,'father':52,'brother':17,'me':23}
print('uncle' in info) 返回FALSE
print('me' in info) 返回True
info.get('me'): 查看‘me’的值是多少,不存在返回NONE
print(info.get('me')) 返回23
print(info.get('uncle')) 返回None
info={'mother':50,'father':52,'brother':17,'me':23}
info.setdefault('cousin',59) 类似info.get() 但是 当cousin不存在时生产新的键值对
>>info={'mother':50,'father':52,'brother':17,'me':23,'cousin':'59'}
but info.setdefault('mother',59)
>>>>info={'mother':50,'father':52,'brother':17,'me':23'}
info.pop('me')
info={'mother':50,'father':52,'brother':17,'me':23}
a=info.pop('me') >>a=23
print(info) info>>info={'mother':50,'father':52,'brother':17}
info={'mother':50,'father':52,'brother':17,'me':23}
a=info.pop('cousin')
print(a)
print(info) 报错,返回 KeyError
相关文章推荐
- python中字典内置函数的用法解析
- Python字典内置函数和方法
- Python基础二——列表,元组,字典,集合
- Python标准库:内置函数super([type[, object-or-type]])
- Python字典的fromkeys方法“疑惑”解析
- python学习1:字典
- [Python学习笔记][Python内置函数]
- Python - 创建字典的多种方式
- python 的内置函数,闭包,以及装饰器
- python关于列表转为字典的两个小方法
- 第六天-python字典-2
- python中字典setdefault()方法理解。
- python实现class对象转换成json/字典的方法
- python实现class对象转换成json/字典的方法
- python 数据类型:字典
- 【python系列】字典的浅拷贝和深拷贝
- Python3之for循环、元组、字典
- python 字典
- python字典多键值及重复键值的使用方法(详解)
- 6 Python字典dict