当前位置 : 主页 > 编程语言 > python >

Python-字典(练习题)

来源:互联网 收集:自由互联 发布时间:2022-06-15
"""练习题"""cities = { 'Wuhan': { 'country': 'China', 'population': '1700', 'fact': 'nice' }, 'New York':{ 'country': 'America', 'population': '1600', 'fact': 'good' }, 'Paris':{ 'country': 'French', 'population': '1000', 'fact': 'normal
"""练习题"""cities = {
'Wuhan': {
'country': 'China',
'population': '1700',
'fact': 'nice' },
'New York':{
'country': 'America',
'population': '1600',
'fact': 'good'
},
'Paris':{
'country': 'French',
'population': '1000',
'fact': 'normall' }
}
for city_name,city_info in cities.items():
print('city_name:'+city_name)
country = city_info['country']
population = city_info['population']
fact = city_info['fact']
print('country:'+country+'\t'+'population:'+population+'\t'+'fact:'+fact+'\n')

"""添加键和值"""
cities['Wuhan']['history'] = 100
print(cities.items())
"""修改键和值"""
cities['Wuhan']['history'] = 50
print(cities.items())
"""删除键和值"""
del cities['Wuhan']['history']
print(cities.items())
上一篇:Python-while(列表)
下一篇:没有了
网友评论