当前位置:网站首页>Lesson 026: Dictionary: when the index is not easy to use 2 | after class test questions and answers
Lesson 026: Dictionary: when the index is not easy to use 2 | after class test questions and answers
2022-06-22 21:36:00 【ChaseTimLee】
Test questions :
0. Python Does your dictionary support one click (Key) Multivalued (Value)?
answer : I won't support it , Assigning another value to the same key will directly overwrite the previous value .
1. In the dictionary , If you try to create a key that doesn't exist (Key) What happens to the assignment ?
answer : The corresponding key is automatically created (Key) And add the corresponding value (Value) go in .
2. Membership operator (in and not in) You can check whether an element exists in the sequence , Of course, it can also be used to check a key (Key) Whether it exists in the dictionary , What kind of inspection is more efficient ? Why? ?
answer : Check the key in the dictionary (Key) Existence is more efficient than checking the existence of a specified element in a sequence . Because the principle of dictionary is to use hash algorithm to store , One step in place , There is no need to use a lookup algorithm for matching , So the time complexity is O(1), Very efficient .
3. Python Key matching (Key) And the value (Value) There are no type restrictions ?
answer :Python The requirements for keys are relatively strict , They must be hashable (Hash) The object of , Cannot be of variable type ( Including variable 、 list 、 Dictionary itself, etc ).
however Python There is no limit to the value , They can be arbitrary Python object .
4. Please visually check that the following code is executed , Dictionaries dict1 What is the content of ?
>>> dict1.fromkeys((1, 2, 3), ('one', 'two', 'three'))
>>> dict1.fromkeys((1, 3), ' Numbers ')
answer : After execution , Dictionaries dict1 The content is :{1: ‘ Numbers ’, 3: ‘ Numbers ’}
What we should pay attention to here is ,fromkeys The method is to create a new dictionary directly , Don't try to use it to modify an existing dictionary , Because it will cover the whole dictionary directly and ruthlessly .
5. If you need a dictionary dict1 = {1: ‘one’, 2: ‘two’, 3: ‘three’} copy to dict2, What should you do ?
answer : You can use the dictionary copy() Method :dict2 = dict1.copy(), Transfer to... In other languages Python At the beginning, the partners may habitually use the method of assignment directly (dict2 = dict1), Do this in Python Just copy the reference of the object in .
>>> a = {
1:'one', 2:'two', 3:'three'}
>>> b = a.copy()
>>> c = a
>>> c[4] = 'four'
>>> c
{
1: 'one', 2: 'two', 3: 'three', 4: 'four'}
>>> a
{
1: 'one', 2: 'two', 3: 'three', 4: 'four'}
>>> b
{
1: 'one', 2: 'two', 3: 'three'}
use one's hands :
0. Try to write a user login program ( This is an attempt to encapsulate functionality into functions ), The program implementation is shown in the figure :

user_data = {
}
def new_user():
prompt = ' Please enter a user name :'
while True:
name = input(prompt)
if name in user_data:
prompt = ' This user name is already in use , Please re-enter :'
continue
else:
break
passwd = input(' Please input a password :')
user_data[name] = passwd
print(' Registered successfully , Try logging in ^_^')
def old_user():
prompt = ' Please enter a user name :'
while True:
name = input(prompt)
if name not in user_data:
prompt = ' The user name you entered does not exist , Please re-enter :'
continue
else:
break
passwd = input(' Please input a password :')
pwd = user_data.get(name)
if passwd == pwd:
print(' Welcome to XXOO System , Please click... In the upper right corner X End procedure !')
else:
print(' Wrong password !')
def showmenu():
prompt = ''' |--- A new user :N/n ---| |--- Login account :E/e ---| |--- Launch program :Q/q ---| |--- Please enter the instruction code :'''
while True:
chosen = False
while not chosen:
choice = input(prompt)
if choice not in 'NnEeQq':
print(' The command code you entered is incorrect , Please re-enter :')
else:
chosen = True
if choice == 'q' or choice == 'Q':
break
if choice == 'n' or choice == 'N':
new_user()
if choice == 'e' or choice == 'E':
old_user()
showmenu()
边栏推荐
- List of outstanding talents: no crystal vibration, one drag, eight burn and upgrade [chapter]
- CVPR2022 | 海德堡大学《深度视觉相似性与度量学习》教程
- How swiftui simulates the animation effect of view illumination increase
- Android kotlin SP DP to PX
- 浅析 Open API 设计规范
- The second harmonyos application development training
- 第027讲:集合:在我的世界里,你就是唯一 | 课后测试题及答案
- Jerry's music mode obtains the directory of playing files [chapter]
- Fegin的解析
- 优化求解器 | Gurobi的MVar类:矩阵建模利器、求解对偶问题的备选方案 (附详细案例+代码)
猜你喜欢
![[redis]Redis6的主从复制](/img/47/3be33a0d7435bd75cdd6e7b4ea51d4.png)
[redis]Redis6的主从复制

Introduce sparse activation mechanism! Uni perceiver MOE significantly improves the performance of generalist model
![Jerry's music mode obtains the directory of playing files [chapter]](/img/2f/efb8a077e3e398cb3b14cfd98a8422.png)
Jerry's music mode obtains the directory of playing files [chapter]

杰理之外挂 4M 的 flash 在 PC 上查看大小只有 1M 的处理方法【篇】
![[160. cross linked list]](/img/79/177e2c86bd80d12f42b3edfa1974ec.png)
[160. cross linked list]
![[20. valid brackets]](/img/e9/50f327048055b07b68e694a9003130.png)
[20. valid brackets]

杰理之开启四声道通话近端变调问题【篇】

第019讲:函数:我的地盘听我的 | 课后测试题及答案

TC397 Flash

查询es分页下标超过1万
随机推荐
[redis]发布与订阅
大势智慧创建倾斜模型和切割单体化
Share deadlock problems encountered in insert into select (project practice)
Android kotlin SP DP to PX
[513. find the value in the lower left corner of the tree]
5分钟快速上线Web应用和API(Vercel)
Simulated 100 questions and simulated examination of hoisting machinery command examination in 2022
[redis] cluster and common errors
Lesson 020: functions: embedded functions and closures | after class test questions and answers
Final review of scientific and technological literature of Shandong University (Personal Crash Course)
Introduce sparse activation mechanism! Uni perceiver MOE significantly improves the performance of generalist model
csv新增一列
快速排序模板 & 注意事项
LeetCode#20. Valid parentheses
80- paging query, not only writing
72 results and development suggestions of the latest on-site production system optimization
[redis]三种新数据类型
Redis的使用场景分享(项目实战)
DACL output on Jerry's hardware, DAC output sound of left and right channels [chapter]
杰理之开启四声道通话近端变调问题【篇】