第六章 字典
6.1 一个简单的字典
6.2 使用字典
在Python中 字典是一系列 键-值 对。并且将字典放在{}中
6.2.1 访问字典中的值
6.2.2 添加键-值对
6.2.3 先创建一个空字典
使用字典来存储用户提供的数据活在编写能自动生成大量键-值对的代码时 通常都需要定义一个空字典
6.2.4 修改字典中的值
是真的改变了值
6.2.5 删除键-值对
有点懒 哈哈哈哈哈
不过呢 删除也是 永久删除的哈
6.2.6 由类似对象组成的字典
一定一定要注意格式!!!不要漏写逗号!!!
6.2 动手试一试
- 6-1 人
people = {'first_name' : 'sean',
'last_name' : 'yi',
'age' : 20,
'city' : 'kunming'}
print (people)
- 6-2 喜欢的数字
# 假的数据 没有之一
favorite_number = {'yc':9,
'cy':8,
'yyc':7,
'ycc':6,
'yyy':5}
print (favorite_number)
print ("\nYc's favorite_number is " + str(favorite_number['yc']) + ".")
print ("\nCy's favorite_number is " + str(favorite_number['cy']) + ".")
print ("\nYyc's favorite_number is " + str(favorite_number['yyc']) + ".")
print ("\nYcc's favorite_number is " + str(favorite_number['ycc']) + ".")
print ("\nYyy's favorite_number is " + str(favorite_number['yyy']) + ".")
- 6-3 词汇表
不想写完了 但是写法是对的
glossaries = {'algorithm' : '算法',
'reference' : '引用',
'summary' : '摘要',
'alert' : '警告',
'indicate' : '表明,指出'
}
print ('algorithm : ' + str(glossaries["algorithm"]))
6.3 遍历字典
6.3.1 遍历所有的键-值对
6.3.2 遍历字典中的所有键
6.3.3 按顺序遍历字典中的所有键
6.3.4 遍历字典中的所有值
如何做到元素的独一无二呢
6.3 动手试一试
- 6-4 词汇表
glossaries = {'algorithm' : '算法',
'reference' : '引用',
'summary' : '摘要',
'alert' : '警告',
'indicate' : '表明,指出'
}
for k,v in glossaries.items():
print ("编程词汇 : " + k)
print ("含义 : " + v)
- 6-5 河流
rivers = {"NileRiver" : "egypt" ,
"AmazonRiver" : "peru" ,
"ChangjiangRiver" : "china" ,
}
for n,t in rivers.items():
print ("The " + n.title() + " runs through " + t.title() +".")
print (n.title())
print (t.title())
- 6-6 调查
6.4 嵌套
6.4.1 字典列表
6.4.2 在字典中存储列表
6.4.3 在字典中存储字典
还是提示符号不要漏写
该程序两个字典的结构相同 所以不复杂
6.4 动手试一试
- 6-7 人
改信息就好了 太懒了 只写了个意思
yc_0 = {'first_name' : 'sean',
'last_name' : 'yi',
'age' : 20,
'city' : 'kunming'}
yc_1 = {'first_name' : 'sean',
'last_name' : 'yi',
'age' : 20,
'city' : 'kunming'}
yc_2 = {'first_name' : 'sean',
'last_name' : 'yi',
'age' : 20,
'city' : 'kunming'}
people = [yc_0,yc_1,yc_2]
for yc in people:
print (yc)
- 6-8 宠物
我觉得和上面那个一样
yc_0 = {'type' : 'cat' , 'name' : 'cc' , }
yc_1 = {'type' : 'cat' , 'name' : 'ccc' , }
yc_2 = {'type' : 'cat' , 'name' : 'ccccc' , }
pets = [yc_0,yc_1,yc_2]
for yc in pets:
print (yc)
- 6-9 喜欢的地方
favorite_places = {
'yc' : ['paris','rome' ],
'ycc' : ['rome' , 'paris'],
}
for name,places in favorite_places.items():
print ("\n" + name.title() + "'s favorite places are: ")
for place in places:
print ("\t" + place.title())
- 6-10 喜欢的数字
favorite_number = {'yc':'9',
'cy':['9,8,7'],
'yyc':['7,6'],
'ycc':['6,5,4'],
'yyy':['4,3,2,1'],}
print (favorite_number)
for users,numbers in favorite_number.items():
print (users.title() + "'s favorite numbers is : ")
for number in numbers:
print ("\t" + str(number))
print ("Yc's favorite_number is " + str(favorite_number['yc']) + ".")
print ("Cy's favorite_number is " + str(favorite_number['cy']) + ".")
print ("Yyc's favorite_number is " + str(favorite_number['yyc']) + ".")
print ("Ycc's favorite_number is " + str(favorite_number['ycc']) + ".")
print ("Yyy's favorite_number is " + str(favorite_number['yyy']) + ".")
- 6-11 城市
cities = {
'city_0' : {
'county' : 'one' ,
'population' : '10' ,
'fact' : '人不多' ,
},
'city_1' : {
'county' : 'two' ,
'population' : '10000' ,
'fact' : '人有点多' ,
},
'city_2' : {
'county' : 'three' ,
'population' : '10000000' ,
'fact' : '人很多' ,
},
}
for city,introduction in cities.items():
city = introduction['county']
fact = introduction['fact']
population = introduction['population']
print ("\n" + city.title() + "的" + fact +
"人口数为 : " + population )
- 6-12 扩展
不写了懒