当前位置:网站首页>Lesson 025: Dictionary: after class test questions and answers when the index is not easy to use
Lesson 025: Dictionary: after class test questions and answers when the index is not easy to use
2022-06-22 21:36:00 【ChaseTimLee】
Test questions :
0. When you hear your friends talking about “ mapping ”、“ Hash ”、“ hash ” perhaps “ Relational arrays ” When , In fact, what are they talking about ?
answer : Yes , As a matter of fact, they are discussing our introduction “ Dictionaries ”, It's all a concept !( Bear in mind , loading X The essence of is to say the same thing as different things ~)
1. Try putting the data (‘F’: 70, ‘C’: 67, ‘h’: 104, ‘i’: 105, ‘s’: 115) Create as a dictionary and access key ‘C’ Corresponding value ?
>>> MyDict = dict((('F', 70), ('i',105), ('s',115), ('h',104), ('C',67)))
>>> MyDict_2 = {
'F':70, 'i':105, 's':115, 'h':104, 'C':67}
>>> type(MyDict)
<class 'dict'>
>>> type(MyDict_2)
<class 'dict'>
>>> MyDict['C']
67
2. In square brackets (“[]”) The enclosed data is called a list , Then use braces (“{}”) The enclosed data is called dictionary , Am I right? ?
answer : incorrect .
>>> NotADict = {
1, 2, 3, 4, 5}
>>> type(NotADict)
<class 'set'>
3. How do you understand that there are some things a dictionary can do , but “ Omnipotent ” Lists are hard to implement ( I can't do it T_T)?
>>> brand = [' Lining ', ' Nike ', ' Adidas ', ' fish C Studio ']
>>> slogan = [' Anything is possible ', 'Just do it', 'Impossible is nothing', ' Let programming change the world ']
>>> print(' fish C The slogan of the studio is :', slogan[brand.index(' fish C Studio ')])
fish C The slogan of the studio is : Let programming change the world
>>> dict1 = {
' Lining ':' Anything is possible ', ' Nike ':'Just do it', ' Adidas ':'Impossible is nothing', ' fish C Studio ':' Let programming change the world '}
>>> print(' fish C The slogan of the studio is :', dict1[' fish C Studio '])
fish C The slogan of the studio is : Let programming change the world
4. The following code , Are they all doing the same thing ? Can you see the difference ?
>>> a = dict(one=1, two=2, three=3)
>>> b = {
'one': 1, 'two': 2, 'three': 3}
>>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
>>> d = dict([('two', 2), ('one', 1), ('three', 3)])
>>> e = dict({
'three': 3, 'one': 1, 'two': 2})
answer : Yes , They are all creating dictionaries :a = dict(one=1, two=2, three=3), Uh , I can't see the difference ~
5. Pictured , Can you guess the code with the mosaic part ?

data = "1000, Little turtle , male "
MyDict = {
}
# Remember how to split strings , Don't forget after learning ^_^
(MyDict['id'], MyDict['name'], MyDict['sex']) = data.split(',')
print("ID: " + MyDict['id'])
print("Name: " + MyDict['name'])
print("Sex " + MyDict['sex'])
use one's hands :
0. Try to use the characteristics of the dictionary to write an address book program , The function is shown in the figure :

print('|--- Welcome to the address book program ---|')
print('|--- 1: Query contact information ---|')
print('|--- 2: Insert a new contact ---|')
print('|--- 3: Delete existing contacts ---|')
print('|--- 4: Quit the address book program ---|')
contacts = dict()
while 1:
instr = int(input('\n Please input the relevant instruction code :'))
if instr == 1:
name = input(' Please enter the contact name :')
if name in contacts:
print(name + ' : ' + contacts[name])
else:
print(' The name you entered is no longer in the address book !')
if instr == 2:
name = input(' Please enter the contact name :')
if name in contacts:
print(' The name you entered already exists in the address book -->> ', end='')
print(name + ' : ' + contacts[name])
if input(' Whether to modify user profile (YES/NO):') == 'YES':
contacts[name] = input(' Please enter the user's contact number :')
else:
contacts[name] = input(' Please enter the user's contact number :')
if instr == 3:
name = input(' Please enter the contact name :')
if name in contacts:
del(contacts[name]) # You can also use dict.pop()
else:
print(' The contact you entered does not exist .')
if instr == 4:
break
print('|--- Thank you for using the address book program ---|')
边栏推荐
- Introduce sparse activation mechanism! Uni perceiver MOE significantly improves the performance of generalist model
- 第028讲:文件:因为懂你,所以永恒 | 课后测试题及答案【无标题】
- Objective-C byte size occupied by different data types
- csv新增一列
- 长安旗下阿维塔科技增资扩股落定:宁德时代将持股约24%!
- 引入稀疏激活机制!Uni-Perceiver-MoE显著提升通才模型的性能
- kali2021安装RTL8188GU无线网卡[TL-WN726N]驱动
- 73- find the SQL example during the business peak period (report development class)
- php 镜像制作
- 万字长文 | 使用 RBAC 限制对 Kubernetes 资源的访问
猜你喜欢
![[redis]redis persistence](/img/83/9af9272bd485028062067ee2d7a158.png)
[redis]redis persistence

Simulated 100 questions and simulated examination of hoisting machinery command examination in 2022

2022 group programming TIANTI race L1

Lesson 014-15: string (see lesson 27-32 of the new version of little turtle) | after class test questions and answers

第026讲:字典:当索引不好用时2 | 课后测试题及答案
![Jerry's problem of opening the near end of four channel call [chapter]](/img/54/d74a90e37deb2d3929f019d695f9ee.png)
Jerry's problem of opening the near end of four channel call [chapter]

CVPR2022 | 海德堡大学《深度视觉相似性与度量学习》教程
![[the penultimate node in the linked list]](/img/b2/be3b0611981dd0248b3526e8958386.png)
[the penultimate node in the linked list]
![kali2021安装RTL8188GU无线网卡[TL-WN726N]驱动](/img/29/8dd188cc4e909562862b5f2c57c898.png)
kali2021安装RTL8188GU无线网卡[TL-WN726N]驱动
![[redis] publish and subscribe](/img/50/0c2fbbb8f56fccdd3222b77efdd723.png)
[redis] publish and subscribe
随机推荐
第029讲:文件:一个任务 | 课后测试题及答案
第026讲:字典:当索引不好用时2 | 课后测试题及答案
LeetCode#20.有效的括号
第025讲:字典:当索引不好用时 | 课后测试题及答案
鸿蒙第三次培训
牛客 52次月赛 C 说谎的机器 (区间赋值操作由O(n^2)转为O(n)的复杂度)
[the penultimate node in the linked list]
Adblock屏蔽百度热搜
Jerry's near end tone change problem of opening four channel call [chapter]
快速排序模板 & 注意事项
View Apple product warranty status
TC397 Flash
2022 chemical automation control instrument examination exercises and online simulation examination
第032讲:异常处理:你不可能总是对的 | 课后测试题及答案
PlainSelect. getGroupBy()Lnet/sf/jsqlparser/statement/select/GroupByElement;
79- do not create desc descending index when you see order by XXX desc - there is book donation benefit at the end of the article
第019讲:函数:我的地盘听我的 | 课后测试题及答案
2022年起重机械指挥考试模拟100题及模拟考试
[redis] cluster and common errors
安卓kotlin sp dp转px