#!/usr/bin/python
# -*- coding:utf-8 -*-
# created by yuki
#
test={'name': 'yuki', 'age': 18}
print(type(test),len(test))
print(test.keys()) #输出字典所有键
print(test.values()) #输字典出所有值
运行结果如下:
<class 'dict'> 2
dict_keys(['name', 'age'])
dict_values(['yuki', 18])
#!/usr/bin/python
# -*- coding:utf-8 -*-
# created by yuki
#
test={'name': 'yuki', 'age': 18}
print(test,type(test))
str1=str(test)
print(str1,type(str1))
运行结果如下:
{'name': 'yuki', 'age': 18} <class 'dict'>
"{'name': 'yuki', 'age': 18}" <class 'str'>
#!/usr/bin/python
# -*- coding:utf-8 -*-
# created by yuki
#
test={'name': 'yuki', 'age': 18}
print(type(test),test,id(test))
运行结果如下:
<class 'dict'> {'name': 'yuki', 'age': 18} 30968568
#!/usr/bin/python
# -*- coding:utf-8 -*-
# created by yuki
#
test={'name': 'yuki', 'age': 18}
print(type(test),test)
test.clear()
print(type(test),test)
运行结果如下:
<class 'dict'> {'name': 'yuki', 'age': 18}
<class 'dict'> {}
#!/usr/bin/python
# -*- coding:utf-8 -*-
# created by yuki
#
dict1={'name': 'yuki', 'age': 18}
print("dict1:",dict1,id(dict1))
dict2=dict1 #直接赋值
print("dict2:",dict2,id(dict2))
dict3=dict1.copy()
print("dict3:",dict3,id(dict3))
print('--------------分割线-----------------')
#修改dict1的数据
dict1['name']='blackcat'
print("dict1:",dict1,id(dict1))
print("dict2:",dict2,id(dict2))
print("dict3:",dict3,id(dict3))
运行结果如下:
dict1: {'name': 'yuki', 'age': 18} 30640888
dict2: {'name': 'yuki', 'age': 18} 30640888
dict3: {'name': 'yuki', 'age': 18} 30640960
--------------分割线-----------------
dict1: {'name': 'blackcat', 'age': 18} 30640888
dict2: {'name': 'blackcat', 'age': 18} 30640888
dict3: {'name': 'yuki', 'age': 18} 30640960
#!/usr/bin/python
# -*- coding:utf-8 -*-
# created by yuki
#
tuple1=('name','age','gender')
print(tuple1,type(tuple1))
test=dict.fromkeys(tuple1)
print(test,type(test))
test=dict.fromkeys(tuple1,12)
print(test,type(test))
运行结果如下:
('name', 'age', 'gender') <class 'tuple'>
{'name': None, 'age': None, 'gender': None} <class 'dict'>
{'name': 12, 'age': 12, 'gender': 12} <class 'dict'>
key – 字典中要查找的键。
default – 如果指定的键不存在时,返回自己设定的默认值,如果没有设定,则返回None)。
#!/usr/bin/python
# -*- coding:utf-8 -*-
# created by yuki
#
test={'name': 'yuki', 'age': 18}
print(test,type(test))
print(test.get('name'))
print(test.get('gender'))
print(test.get('sex','女'))
运行结果如下:
{'name': 'yuki', 'age': 18} <class 'dict'>
yuki
None
女
key – 查找的键值。
default – 键不存在时,设置的默认键值。
#!/usr/bin/python
# -*- coding:utf-8 -*-
# created by yuki
#
test={'name': 'yuki', 'age': 18}
print(type(test),test)
test.setdefault('gender')
print(type(test),test)
test.setdefault('sex','女')
print(type(test),test)
运行结果如下:
<class 'dict'> {'name': 'yuki', 'age': 18}
<class 'dict'> {'name': 'yuki', 'age': 18, 'gender': None}
<class 'dict'> {'name': 'yuki', 'age': 18, 'gender': None, 'sex': '女'}
#!/usr/bin/python
# -*- coding:utf-8 -*-
# created by yuki
#
dict1={'name': 'yuki', 'age': 18}
print(type(dict1),dict1)
dict2={'gender': '女'}
dict1.update(dict2)
print(type(dict1),dict1)
运行结果如下:
<class 'dict'> {'name': 'yuki', 'age': 18}
<class 'dict'> {'name': 'yuki', 'age': 18, 'gender': '女'}
key – 要在字典中查找的键。
#!/usr/bin/python
# -*- coding:utf-8 -*-
# created by yuki
#
test={'name': 'yuki', 'age': 18}
#检测键 age 是否存在
if 'age' in test:
print("键 age 存在")
else:
print("键 age 不存在")
#检测键 gender 是否存在
if 'gender' in test:
print("键 gender 存在")
else:
print("键 gender 不存在")
#检测键 sex 是否存在
if 'sex' not in test:
print("键 sex 不存在")
else:
print("键 sex 存在")
运行结果如下:
键 age 存在
键 gender 不存在
键 sex 不存在
#!/usr/bin/python
# -*- coding:utf-8 -*-
# created by yuki
#
test={'name': 'yuki', 'age': 18,'gender':'女'}
print(test,type(test))
result=test.items()
print(result,type(result))
运行结果如下:
{'name': 'yuki', 'age': 18, 'gender': '女'} <class 'dict'>
dict_items([('name', 'yuki'), ('age', 18), ('gender', '女')]) <class 'dict_items'>
注意:Python2.x 是直接返回列表。
#!/usr/bin/python
# -*- coding:utf-8 -*-
# created by yuki
#
test={'name': 'yuki', 'age': 18,'gender':'女'}
print(type(test),test)
result=test.keys()
print(type(result),result)
list1=list(result) # 转换为列表
print(type(list1),list1)
运行结果如下:
<class 'dict'> {'name': 'yuki', 'age': 18, 'gender': '女'}
<class 'dict_keys'> dict_keys(['name', 'age', 'gender'])
<class 'list'> ['name', 'age', 'gender']
#!/usr/bin/python
# -*- coding:utf-8 -*-
# created by yuki
#
test={'name': 'yuki', 'age': 18,'gender':'女'}
print(type(test),test)
result=test.values()
print(type(result),result)
list1=list(result) # 转换为列表
print(type(list1),list1)
运行结果如下:
<class 'dict'> {'name': 'yuki', 'age': 18, 'gender': '女'}
<class 'dict_values'> dict_values(['yuki', 18, '女'])
<class 'list'> ['yuki', 18, '女']
#!/usr/bin/python
# -*- coding:utf-8 -*-
# created by yuki
#
test={'name': 'yuki', 'age': 18,'gender':'女'}
print(type(test),test)
result=test.pop('name')
print(type(result),result)
print(type(test),test)
运行结果如下:
<class 'dict'> {'name': 'yuki', 'age': 18, 'gender': '女'}
<class 'str'> yuki
<class 'dict'> {'age': 18, 'gender': '女'}
如果字典已经为空,却调用了此方法,就报出KeyError异常。
#!/usr/bin/python
# -*- coding:utf-8 -*-
# created by yuki
#
test={'name': 'yuki', 'age': 18,'gender':'女'}
print(test,type(test))
test.popitem()
print(test,type(test))
test.popitem()
print(test,type(test))
test.popitem()
print(test,type(test))
test.popitem()
print(test,type(test))
运行结果如下:
{'name': 'yuki', 'age': 18, 'gender': '女'} <class 'dict'>
{'name': 'yuki', 'age': 18} <class 'dict'>
{'name': 'yuki'} <class 'dict'>
{} <class 'dict'>
Traceback (most recent call last):
File "E:/softwares/python/pythonProject/day20200802/helloWorld.py", line 19, in <module>
test.popitem()
KeyError: 'popitem(): dictionary is empty'