5. Python映射:字典dict
5.1 字典dict基本概念(重点)
>>> #申明字典
>>> #1. 直接申明
>>> dic = {'a':1, 'b':2}
>>> dic
{'a': 1, 'b': 2}
>>> #2. dict()函数
>>> dic2 = dict(m=10, n='aaa', h=100)
>>> dic2
{'n': 'aaa', 'm': 10, 'h': 100}
>>> #3. 通过列表
>>> list1 = [('a', 1), ('b', 2)]
>>> list2 = [['a', 1], ['b', 2]]
>>> list3 = (['a', 1], ['b', 2])
>>> dict(list1)
{'a': 1, 'b': 2}
>>> dict(list2)
{'a': 1, 'b': 2}
>>> dict(list3)
{'a': 1, 'b': 2}
>>> #4. 空值dict
>>> keys = ['a', 'b', 'c']
>>> m = dict.fromkeys(keys)
>>> m
{'a': None, 'c': None, 'b': None}
5.2 dict字典的元素访问及遍历
>>> #字典是否包含key
>>> m = {'a':1, 'b':2, 'c':3}
>>> 'b' in m
True
>>> 'g' in m
False
>>> #查看一个元素的value
>>> m = list(range(9))
>>> m
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> m[2]
2
>>> m = {'a':1, 'b':2, 'c':3}
>>> m['a']
1
>>> m['c']
3
>>>
>>>
>>> poi = {'name':'shop', 'city':'shanghai', 'information':{'address':'somwhere', 'num':6633}}
>>> poi['information']
{'num': 6633, 'address': 'somwhere'}
>>> poi['information']['address']
'somwhere'
>>>
>>> #查看元素方法2
>>> m = {'a':1, 'b':2, 'c':3}
>>> m.get('a')
1
>>> m.get('d')
>>> m.get('d',print('no-key'))
no-key
>>> m.get('a',print('no-key'))
no-key
1
>>> #查看全部key
>>> poi = {'name':'shop', 'city':'shanghai', 'information':{'address':'somwhere', 'num':6633}}
>>> poi.keys()
dict_keys(['name', 'city', 'information'])
>>> type(poi.keys())
<class 'dict_keys'>
>>> k = list(poi.keys())
>>> k
['name', 'city', 'information']
>>> poi.values()
dict_values(['shop', 'shanghai', {'num': 6633, 'address': 'somwhere'}])
>>> type(poi.values())
<class 'dict_values'>
>>> v = list(poi.values())
>>> v
['shop', 'shanghai', {'num': 6633, 'address': 'somwhere'}]
>>> #返回字典全部元素
>>> poi.items()
dict_items([('name', 'shop'), ('city', 'shanghai'), ('information', {'num': 6633, 'address': 'somwhere'})])
>>> m.items()
dict_items([('a', 1), ('c', 3), ('b', 2)])
>>> type(m.items())
<class 'dict_items'>
>>> i = list(m.items())
>>> i
[('a', 1), ('c', 3), ('b', 2)]
>>>
>>>
>>> poi
{'name': 'shop', 'city': 'shanghai', 'information': {'num': 6633, 'address': 'somwhere'}}
>>> for i in poi.keys():
print(i)
name
city
information
>>> for i in poi.values():
print(i)
shop
shanghai
{'num': 6633, 'address': 'somwhere'}
>>> for (k, v) in poi.items():
print('{} - {}'.format(k, v))
name - shop
city - shanghai
information - {'num': 6633, 'address': 'somwhere'}
5.3 dict字典的常用操作(重点)
>>> #修改一个值
>>> m = [1, 2, 3, 4, 5, 6]
>>> m[2]=123
>>> m[2]
123
>>> m
[1, 2, 123, 4, 5, 6]
>>> n = {'a':1, 'v':2, 'c':5}
>>> n['a']=99
>>> n
{'a': 99, 'c': 5, 'v': 2}
>>> n['c']=[1, 2, 3]
>>> n
{'a': 99, 'c': [1, 2, 3], 'v': 2}
>>> #删除一个值
>>> del n['c']
>>> n
{'a': 99, 'v': 2}
>>> del n['a']
>>> n
{'v': 2}
>>> #更新/合并一个键值对
>>> n2 = {'name':'tom', 'gender':'female'}
>>> n.update(n2)
>>> n
{'name': 'tom', 'gender': 'female', 'v': 2}
>>>
>>>
>>> a= {'m':1, 'n':2, 'p':3}
>>> a
{'n': 2, 'p': 3, 'm': 1}
>>> b = a
>>> b
{'n': 2, 'p': 3, 'm': 1}
>>> a.update({'a':'tom'})
>>> a
{'a': 'tom', 'n': 2, 'p': 3, 'm': 1}
>>> print(b)
{'a': 'tom', 'n': 2, 'p': 3, 'm': 1}
>>> #通过dict.copy()来复制
>>> a= {'m':1, 'n':2, 'p':3}
>>> b = a.copy()
>>> del a['p']
>>> a
{'n': 2, 'm': 1}
>>> b
{'n': 2, 'p': 3, 'm': 1}
>>>
>>> #弹出
>>> a.pop('m')
1
>>> a
{'n': 2}
>>> b.popitem()
('n', 2)
>>> a= {'m':1, 'n':2, 'p':3}
>>> a.popitem()
('n', 2)
name = ['小明','小红','小王']
num = ['A01', 'A02', 'A03']
score = [90, 80, 70]
k = ['姓名', '成绩', '学号']
st1 = dict.fromkeys(k)
#print(st1)
st = [st1, st1.copy(), st1.copy()]
#print(st)
S = []
for i in st:
n = st.index(i)
i['姓名'] = name[n]
i['学号'] = num[n]
i['成绩'] = score[n]
print(st)
网友评论