python 入门到实践第六章练习题


user_info = {
'first_name':'alex',
'last_name':'wei',
'age':'31',
'city':'cq',
}#创建一个字典

print("用户的姓是:" + user_info['first_name'])#打印字典内的first_name健对于到的值
print("用户的名是:" + user_info['last_name'])
print("用户的全名是:" + user_info['first_name'] + user_info['last_name'])
print("用户的年龄是:" + user_info['age'] + "岁")
print("用户的居住城市是:" + user_info['city'])

print("\n")

'''6-2'''
names = ['alex','eric','toto','pipi','yang']
fave_nums = {
'alex':5,
'eric':8,
'toto':12,
'pipi':99,
'yang':125,
}

for i in names:
print(i + ":" + str(fave_nums[i]))

print("\n")
'''6-3'''

zidian = {
'print':'打印',
'list':'将内容写进列表',
'range':'范围',
'in':'在什么里面',
'not in':'不在范围内',
}

print("print:" + "\n" + zidian['print'])
print("list:" + "\n" + zidian['list'])
print("range:" + "\n" + zidian['range'])
print("in:" + "\n" + zidian['in'])
print("not in:" + "\n" + zidian['not in'])

'''6-4'''

zidian = {
'print':'打印',
'list':'将内容写进列表',
'range':'范围',
'in':'在什么里面',
'not in':'不在范围内',
}#创建一个字典

zidian['for'] = '循环'#新增一个键-值到字典中
zidian['if'] = '判断'#新增一个键-值到字典中
zidian['set'] = '集合'#新增一个键-值到字典中
zidian['else'] = '否则判断'#新增一个键-值到字典中

print("\n")
for word,meaning in zidian.items():#用items方法遍历字典
print(word + ":" + meaning)

print("\n")


'''6-5'''

rivers = {
'西藏':'长江',
'四川':'长江',
'重庆':'嘉陵江',
'黑龙江':'黑龙江',
'青海':'黑龙江',
'中国':'黄河',
}#创建一个字典,特别注意字典的键不能重复,值可以重复

for river , diqu in rivers.items():#用items的方法遍历整个字典,并传给变量
print(diqu + "流经" + river )

print("\n")

for river in rivers.values():
print(river)

print("\n")

for key in rivers.keys():
print(key)


'''6-7'''
alex = {'fistname':'alex','lastname':'wei','age':31,'city':'cq'}#创建一个字典
cres = {'fistname':'crys','lastname':'peng','age':28,'city':'sc'}
eric = {'fistname':'eric','lastname':'feng','age':29,'city':'bj'}

people = [alex,cres,eric]#列表中存字典

for name in people:#遍历整个了列表,返回的值为字典
#print(name)
full_name = name['fistname'] + name['lastname']#返回的是字典,所以用字典的方法取值
print(full_name+":")
for name_a , info in name.items():
print("\t"+name_a+":"+str(info))
#print(info)
print(people[0:1])

favprote_places = {
'alex':['cq','sc','xz'],#创建一个字典,并把列表存入字典中
'cres':['bq','cq','sh'],
'eric':['gz','cd','cq'],
}

'''6-9'''
print("\n")
for name , city in favprote_places.items():#定义两个遍历把字典中的键值对存入变量中
print(name+":")
for i in city:#由于字典中的值存的是列表,所以可以用列表的方法来处理数据
print("\t"+i)
'''6-10'''


fave_nums = {
'alex':[2,5],
'eric':[8],
'toto':[12,11],
'pipi':[99,88],
'yang':[125],
}

print("\n")
for name_b , number in fave_nums.items():
if len(number) > 1:#给一个判断,用len函数判断number返回的值是否有多个
print(name_b + ",喜欢多个数字,他喜欢的数字是:" )
for i in number:#用for循环取回字典中列表的值
print(i)
else:
print(name_b + ",喜欢只一个数字,他喜欢的数字是:")
for i in number:
print(i)

'''6-11'''

print("\n")

cities = {
'重庆':{
'地区':'西部',
'人口':'3000千万',
'经济':'发展迅速',
},
'四川' : {
'地区':'西部',
'人口':'8500万',
'经济':'发展迅速'
},
'北京' : {
'地区':'首都',
'人口':'2300万',
'经济':'非常好'
},
}#创建一个名为cities的字典,指点内键值对为别为3个字典,这种是字典中嵌套字典.


for citi_name , citi_info in cities.items():#用for 循环遍历cities的键值对,传给2个变量,其中citi_info传回去的是一个字典
print("城市名称为:" + citi_name)
for info , vales in citi_info.items():#由于citi_info 是一个字典所以用字典的方式取值到变量
print("\t" + info + ":" + vales )#打印字典中的值\t为打印制表符
 
posted @ 2020-06-19 15:34  网工学搬砖  阅读(183)  评论(0编辑  收藏  举报