get新技能:
1. items() :以列表返回可遍历的(键, 值) 元组数组,例如:↓↓↓ ('name' : 'CX') 是元组
dick = {'name':'CX','sex':'Man','age':'26'}
print(dick.items())
'''实际输出结果 >>>[('name':'CX'),('sex':'Man'),('age':'26')]
2. get() :返回指定键的值,如果值不在字典中返回默认值,语法:dict.get(key, default=None):
- key -- 字典中要查找的键。
- default -- 如果指定键的值不存在时,返回该默认值。
例如:↓↓↓
dick = {'name':'CX','sex':'Man','age':'26'}
print(dick.get('sex'))
'''实际输出 >>>Man '''
print(dick.get('height'))
'''实际输出 >>>None '''
正文:
接下来就是学习Python的‘字典’数据结构了,字典是类似列表的一种存储数据的方法。字典可以将一样东西和另一样东西关联,不管它们的类型是什么都行,所以你可以通过任何东西找到字典中的元素,而列表则只能通过数值来获取元素。
我们来比较下列表的功能:↓↓↓
things = ['a', 'b', 'c', 'd']
print(things[1])
'''实际输出 >>>b '''
things[1] = 'z'
print(things[1])
'''实际输出 >>>z '''
things
'''实际输出 >>>['a', 'z', 'c', 'd'] '''
列表只能通过数值来获取元素,可以通过数值来替换相对应索引的元素,但不能想字典一样直接添加元素。
现在再来看下字典的功能:↓↓↓
stuff = {'name':'Zed','age':'39','height':6*12+2}
print(stuff['name'])
'''实际输出 >>>Zed '''
print(stuff['age'])
'''实际输出 >>>39 '''
print(stuff['height'])
'''实际输出 >>>74 '''
#在字典中添加元素
stuff['city'] = 'SF'
print(stuff['city'])
'''实际输出 >>>SF '''
stuff
'''实际输出 >>>{'name':'Zed','age':'39','height':74,'city':'SF'} '''
字典可以通过任何类型的key(键)来获取value(值),也可以直接替换和添加这些值,例如:↓↓↓
stuff[1] = 'WOW'
stuff[2] = 'Neato'
print(stuff[1])
'''实际输出 >>>WOW '''
print(stuff[2])
'''实际输出 >>>Neato '''
stuff
'''实际输出 >>{'name':'Zed','age':'39','height':74,'city':'SF',1:'WOW',2:'Neato'} '''
然后我们还可以从字典或列表中删除元素,就是使用 del 方法:↓↓↓
'''删除列表中的元素'''
del things[1]
print(things)
'''实际输出 >>>['a','c','d'] '''
'''删除字典中的元素'''
del stuff[1]
print(stuff)
'''实际输出 >>>{'name':'Zed','age':'39','height':6*12+2,'city':'SF',2:'Neato'} '''
del stuff['city']
print(stuff)
'''实际输出 >>>{'name':'Zed','age':'39','height':74,2:'Neato'} '''
基础练习:
# create a mapping of state to abbreviation
# 创建状态到缩写的映射
states = {
'Oregon': 'OR',
'Florida': 'FL',
'California': 'CA',
'New York': 'NY',
'Michigan': 'MI'
}
# create a basic set of states and soe cities in them
cities = {
'CA': 'San Francisco',
'MI': 'Detroit',
'FL': 'Jacksonville'
}
# add some more cities
cities['NY'] = 'New York'
cities['OR'] = 'Portland'
# print out some cities
print('-' * 50,'1')
print('NY State has: ', cities['NY'])
print('OR State has: ', cities['OR'])
# print some states
print('-' * 50,'2')
print("Michigan's abbreviation is: ", states['Michigan'])
print("Florida's abbreviation is: ", states['Florida'])
#do it by using the state abbreviation
print('-' * 50,'3')
print("Michigan has: ", cities[states['Michigan']])
print("Florida has: ", cities[states['Florida']])
#print every state abbreviation
print('-' * 50,'4')
for state, abbrev in list(states.items()):
print(f"{state} is abbreviated {abbrev}")
#print every city in state
print('-' * 50,'5')
for abbrev, city in list(cities.items()):
print(f"{abbrev} has the city {city}")
# now do both at the same time
print('-' * 50,'6')
for state, abbrev in list(states.items()):
print(f"{state} state is abbreviated {abbrev}")
print(f"and has city {cities[abbrev]}")
print('-' * 50,'7')
# safely get a abbreviation by state that might not be there
state = states.get('Texas')
if not state:
print('Sorry, no Texas')
# get a city with a default values
city = cities.get('TX','Does Not Exist')
print(f"The city for the state 'TX' is: {city}")