第六章

6-1:

friend = { ' first_name ': ' YIngxue ', ' last_name ': ' Zheng ', ' age ': 20, ' city ': ' Hefei '}
print (friend)


6-2:

numbers = { ' Alan ': 1, ' Mary ': 2, ' John ': 3, ' James ': 4, ' Jay ': 5}
print ( ' Alan ' , numbers[ ' Alan ' ])
print ( ' Mary ' , numbers[ ' Mary ' ])
print ( ' John ' , numbers[ ' John ' ])
print ( ' James ' , numbers[ ' James ' ])
print ( ' Jay ' , numbers[ ' Jay ' ])


6-3:

vo = {
' del ': ' 删除 ',
' lower() ': ' 将所有字母小写 ',
' upper() ': ' 将所有字母大写 ',
' append() ': ' 添加一个元素 ',
' print() ': ' 输出函数 '
}
for key, value in vo. items ():
print (key + ' : ' , value)


6-5:

rivers = { ' Nile ': ' Egyt ', ' Rhine ': ' France ', ' Yellow River ': ' China '}
for key, value in rivers. items ():
print ( ' The ' , key, ' runs through ' , value)
for key in rivers. keys ():
print (key)
for value in rivers. values ():
print (value)


6-6:

favorite_languages = {
' jen ': ' python ',
' sarah ': ' c ',
' edward ': ' ruby ',
' phil ': ' python ',
}
someones = [ ' jen ', ' sarah ', ' phil ', ' jay ', ' john ', ' alan ' ]
for person in someones:
if person in favorite_languages. keys ():
print (person, ' 谢谢你的参与! ' )
else:
print (person, ' 希望你也能参与! ' )


6-7:

friend1 = { ' first_name ': ' YIngxue ', ' last_name ': ' Zheng ', ' age ': 20, ' city ': ' Hefei '}
friend2 = { ' first_name ': ' Xinrui ', ' last_name ': ' Li ', ' age ': 19, ' city ': ' Guangzhou '}
friend3 = { ' first_name ': ' Jie ', ' last_name ': ' Zong ', ' age ': 20, ' city ': ' Guangzhou '}
people = [friend1, friend2, friend3 ]
for person in people:
for key, value in person. items ():
print (key, ' : ' , value, end = ' ' )
print ( ' \n ' )


6-9:

favorite_places = {
' Alan ': [ ' Shanghai ', ' Guangzhou ', ' Shanxi ' ],
' John ': [ ' Beijing ', ' Wuhan ', ' Changsha ' ],
' Mary ': [ ' Hefei ', ' Shijiazhuang ', ' Nanjing ' ]
}
for key, places in favorite_places. items ():
print (key, ' want to go to ' , end = ' ' )
for place in places:
print (place, end = ' ' )
print ()


6-10:

numbers = {
' Alan ': [ 1, 2, 3, 4, 5 ],
' Mary ': [ 6, 7, 8, 9, 10 ],
' John ': [ 11, 12, 13, 14, 15 ],
' James ': [ 16, 17, 18, 19, 20 ],
' Jay ': [ 21, 22, 23, 24, 25 ]
}
for key, places in numbers. items ():
print (key, ' like ' , end = ' ' )
for place in places:
print (place, end = ' ' )
print ()


6-11:

cities = {
' Beijing ': {
' nation ': ' China ',
' population ': 10000000,
' fact ': ' 中国的首都 '
},
' Guangzhou ': {
' nation ': ' China ',
' population ': 10000000,
' fact ': ' 位于中国南部 '
},
' Hefei ': {
' nation ': ' China ',
' population ': 10000000,
' fact ': ' 位于中国中部 '
}
}
for city, info in cities. items ():
print (city, ' : ' )
for key, value in info. items ():
print ( ' \t ' , key, ' : ' , value)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值