Python中如何编写字典练习题?

2026-06-10 23:407阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计149个文字,预计阅读时间需要1分钟。

Python中如何编写字典练习题?

练习题:cities={'Wuhan': {'country': 'China', 'population': '1700', 'fact': 'nice'}, 'New York': {'country': 'America', 'population': '1600', 'fact': 'good'}, 'Paris': {'country': 'French', 'population': '1000', 'fact': 'normal'}}

Python中如何编写字典练习题?

"""练习题"""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())

本文共计149个文字,预计阅读时间需要1分钟。

Python中如何编写字典练习题?

练习题:cities={'Wuhan': {'country': 'China', 'population': '1700', 'fact': 'nice'}, 'New York': {'country': 'America', 'population': '1600', 'fact': 'good'}, 'Paris': {'country': 'French', 'population': '1000', 'fact': 'normal'}}

Python中如何编写字典练习题?

"""练习题"""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())