#因为字典是无序,所以字典没有索引值
#因为字典没有索引值,所以字典以键取值(字典的键相当于列表的索引)
#因为字典以键取值,所以以字典的键唯一并且不可修改
#因为字典的键不可修改,所以可变数据类型不可以给字典做键
>>> info = {'name':'张三','address':'guangzhou','age':19}
>>> info
{'name': '张三', 'address': 'guangzhou', 'age': 19}
>>> type(info)
>>> info[0]
Traceback (most recent call last):
File "", line 1, in
info[0]
KeyError: 0
>>> info['name']
'张三'
>>> info.get('age')
19
>>> info.get('age','height')
19
>>> info.get('age2','height')
'height'
>>> info
{'name': '张三', 'address': 'guangzhou', 'age': 19}
>>> info['like'] = '莲花'
>>> info
{'name': '张三', 'address': 'guangzhou', 'age': 19, 'like': '莲花'}
>>> info['like'] = '菊花'
>>> info
{'name': '张三', 'address': 'guangzhou', 'age': 19, 'like': '菊花'}
>>> del info['like']
>>> info
{'name': '张三', 'address': 'guangzhou', 'age': 19}
#查看dict字典的属性(函数)
>>> dir(dict)
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
#字典的增操作
>>> info = {}
>>> info.fromkeys((1,2,3))
{1: None, 2: None, 3: None}
>>> dict1 = info.fromkeys((1,2,3))
>>> dict1.setdefault('4',40)
40
>>> dict1
{1: None, 2: None, 3: None, '4': 40}
>>> dict1[5] = 50
>>> dict1
{1: None, 2: None, 3: None, '4': 40, 5: 50}
>>> dict2 = dict1.copy()
>>> dict2
{1: None, 2: None, 3: None, '4': 40, 5: 50}
#字典的删操作
>>> info = {'name': '张三', 'address': 'guangzhou', 'age': 19}
>>> info.pop('age')
19
>>> info
{'name': '张三', 'address': 'guangzhou'}
>>> info.popitem()
('address', 'guangzhou')
>>> info
{'name': '张三'}
>>> info = {'name': '张三', 'address': 'guangzhou', 'age': 19}
>>> info.clear()
>>> info
{}
>>> del info
>>> info
Traceback (most recent call last):
File "", line 1, in
info
NameError: name 'info' is not defined
#字典的改操作
>>> info = {'name': '张三', 'address': 'guangzhou', 'age': 19}
>>> info.update(name = '李四')
>>> info
{'name': '李四', 'address': 'guangzhou', 'age': 19}
>>> info.setdefault(6)
>>> info
{'name': '李四', 'address': 'guangzhou', 'age': 19, 6: None}
>>> info.update(6 = 60)
SyntaxError: keyword can't be an expression
>>> info[6] = 60
>>> info
{'name': '李四', 'address': 'guangzhou', 'age': 19, 6: 60}
#字典的查操作
>>> info = {'name': '张三', 'address': 'guangzhou', 'age': 19}
>>> info.keys()
dict_keys(['name', 'address', 'age'])
>>> info.values()
dict_values(['张三', 'guangzhou', 19])
>>> info.items()
dict_items([('name', '张三'), ('address', 'guangzhou'), ('age', 19)])
>>> info['name']
'张三'
#字典的遍历
>>> for i in info: #遍历字典的键
print(i)
name
address
age
6
>>> for i in info.keys(): #遍历字典的键
print(i)
name
address
age
6
>>> for i in info.values(): #遍历字典的值
print(i)
李四
guangzhou
19
60
>>> len(info) #测量字典,有几个键值对
4
>>> for i in info.items(): #遍历字典,以元组形式返回
print(i)
('name', '李四')
('address', 'guangzhou')
('age', 19)
(6, 60)