当前位置:网站首页>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 ---|')
边栏推荐
- Operation of simulation test platform for 2022 Shandong safety officer C certificate test
- 杰理之硬件上 DACL 输出,DAC 输出左右声道的声音【篇】
- Objective-C byte size occupied by different data types
- 513. 找树左下角的值 / 剑指 Offer II 091. 粉刷房子
- php 镜像制作
- 第031讲:永久存储:腌制一缸美味的泡菜 | 课后测试题及答案
- 第021讲:函数:lambda表达式 | 课后测试题及答案
- Introduce sparse activation mechanism! Uni perceiver MOE significantly improves the performance of generalist model
- [513. find the value in the lower left corner of the tree]
- Adblock屏蔽百度热搜
猜你喜欢
![[redis]redis6 transaction operations](/img/50/639867a2fcb92082ea262a8a19bb68.png)
[redis]redis6 transaction operations

(DUC/DDC)数字上混频/正交下混频原理及matlab仿真

杰理之列免晶振一拖八烧录升级【篇】
![[redis]发布与订阅](/img/50/0c2fbbb8f56fccdd3222b77efdd723.png)
[redis]发布与订阅

第029讲:文件:一个任务 | 课后测试题及答案

ICML2022 | 利用虚拟节点促进图结构学习
![[cm11 linked list splitting]](/img/66/6ac3f78db20ec7f177b88c88028dde.png)
[cm11 linked list splitting]

2022 group programming TIANTI race L1

2022化工自动化控制仪表考试练习题及在线模拟考试

Lesson 014-15: string (see lesson 27-32 of the new version of little turtle) | after class test questions and answers
随机推荐
Jerry's plug-in 4m flash to view the processing method with a size of only 1m on the PC [chapter]
Redis usage scenario sharing (project practice)
嵌入式开发基础之任务管理(线程管理)
Baijia forum Daqin rise (lower part)
The problem that Jericho can't play the prompt tone in the music mode using the interrupt mode [chapter]
Android kotlin SP DP to PX
Correspondence between int and char in C language
94-SQL优化案例一则(用到的写法经常是被嫌弃的)
杰理之使用 DP 和 DM 做 IO 按键检测注意点【篇】
[redis] publish and subscribe
DACL output on Jerry's hardware, DAC output sound of left and right channels [chapter]
基于AI驱动大分子药物发现,「华深智药」获近5亿元A轮融资
RuntimeError: CUDA error: CUBLAS_STATUS_EXECUTION_FAILED when calling `cublasSgemmStridedBatched( ha
长安旗下阿维塔科技增资扩股落定:宁德时代将持股约24%!
杰理之列免晶振一拖八烧录升级【篇】
92 match for several_ Recognize SQL write example
基于C语言开发工资管理系统 课程论文+源码及可执行exe文件
2022化工自动化控制仪表考试练习题及在线模拟考试
ACM. HJ24 合唱队 ●●
Lesson 014-15: string (see lesson 27-32 of the new version of little turtle) | after class test questions and answers