当前位置:网站首页>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 ---|')
边栏推荐
- 2022化工自动化控制仪表考试练习题及在线模拟考试
- 为了不曾忘却的纪念:孙剑专题
- An example of 89 Oracle SQL writing and optimizer defects
- Lesson 014-15: string (see lesson 27-32 of the new version of little turtle) | after class test questions and answers
- 300. 最长递增子序列 ●●
- 90- review of several recently optimized Oracle databases
- [206. reverse linked list]
- [redis] profile
- 杰理之AUX 模式使用 AUX1或者 AUX2通道时,程序会复位问题【篇】
- 不同网络结构的特征也能进行对比学习?蚂蚁&美团&南大&阿里提出跨架构自监督视频表示学习方法CACL,性能SOTA!
猜你喜欢

2022化工自动化控制仪表考试练习题及在线模拟考试
How to operate redis on the IntelliJ idea database console

为了不曾忘却的纪念:孙剑专题
![[160. cross linked list]](/img/79/177e2c86bd80d12f42b3edfa1974ec.png)
[160. cross linked list]

Arcgis中las点云数据抽稀

第014-15讲:字符串 (见小甲鱼新版27讲-32讲)| 课后测试题及答案

Share deadlock problems encountered in insert into select (project practice)

软考必备资料大放送,全科目软考资料都给你备好了!

2022危险化学品经营单位主要负责人上岗证题库及模拟考试

2022 a special equipment related management (elevator) examination questions and simulation examination
随机推荐
(DUC/DDC)数字上混频/正交下混频原理及matlab仿真
查询es分页下标超过1万
ByteDance proposes a lightweight and efficient new network mocovit, which has better performance than GhostNet and mobilenetv3 in CV tasks such as classification and detection
第016讲:序列 | 课后测试题及答案
Learning websites that programmers must see
Redis usage scenario sharing (project practice)
杰理之在music模式下提示音使用打断模式无法播放的问题【篇】
杰理之开启四声道通话近端卡顿问题【篇】
kali2021安装RTL8188GU无线网卡[TL-WN726N]驱动
安卓kotlin sp dp转px
[redis]集群与常见错误
RuntimeError: CUDA error: CUBLAS_STATUS_EXECUTION_FAILED when calling `cublasSgemmStridedBatched( ha
杰理之动态切换 EQ 文件【篇】
70 root cause analysis Oracle database sudden performance problems, who will take the blame
嵌入式开发基础之任务管理(线程管理)
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
LeetCode#20.有效的括号
Jerry's plug-in 4m flash to view the processing method with a size of only 1m on the PC [chapter]
Lesson 016: sequence | after class test questions and answers
2022 group programming TIANTI race L1