第六天任务 (【基于Python编程从入门到实践】第六章 字典 书本及动手试一试)

第六章 字典

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 扩展
    不写了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值