[Python brush questions] - Python entry 04 list (below)
🤵♂️ Profile: @Flyme awei
profile 👨💻 About the author:Python
Domain Rising Star Creator.
📒 Series of columns: " Niuke Question Bank - Python" 🌐Recommend "
Niuke.com "——job search tool
|Written Exam Question Bank
|interview experience
|Internship experience
|job hunting
|
👉Click the link to register and study
Niu Ke Question Bank " Online Programming - Python "
python
The learning still has the basic knowledge + do-it-yourself synchronization. in the case ofpython
If you are a novice, you need to find a website where you can practice online. I suggest you go to Niuke.com to practice more.
Getting Started with Python: 04 Lists (Part 2) NP27 - NP31![[Python brush questions] - Python entry 04 list (below) insert image description here](http://img.ifindbug.com/s-img-blog.csdnimg.cn/74b4bd88c7004a5bb72d0896eac3080d.png)
List of common operation methods:
method | describe |
---|---|
ls.append(x) | in the listls add an element at the endx |
ls.insert(i, x) | in the listls Increase the element at the ith position ofx |
ls.clear() | delete listls all elements |
ls.pop(i) | will listls Firsti elements are taken and removed fromls remove the element from |
ls.remove(x) | the first element that appears in the listx delete |
ls.reverse() | will listls Element inversion in |
ls.copy() | generate a new list, copyls all elements in |
Likes of NP27 friends
Description
Niu Niu has onename = ['Niumei', 'YOLO', 'Niu Ke Le', 'Mona']
Recorded the names of his best friends, please create a two-dimensional listfriends
,useappend
function willname
add tofriends
the first line of .
ifNiumei
I like to eat pbehind
, favorite numbers3
,WAY
O's favorite foodin
h, favorite number6
,Niu Ke Le
favorite foodpotato
, favorite numbers0
,Mona
favorite foodbeef
, favorite numbers3
.
Please create a list againfood
Record friends' favorite foods in turn, and use the created listappend
function added tofriends
the second line of;
then create another listnumber
Record your friends' favorite colors in turn, and use the created listappend
function added tofriends
the third line.
sofriends
is a two-dimensionallist
,useprint
The function directly prints this two-dimensionallist
.
Input description:
none
Output description:
[['Niumei', 'YOLO', 'Niu Ke Le', 'Mona'], ['pizza', 'fish', 'potato', 'beef'], [3, 6, 0, 3]]
Code:
name = ['Niumei', 'YOLO', 'Niu Ke Le', 'Mona']
friends = []
friends.append(name)
food = ['pizza', 'fish', 'potato', 'beef']
friends.append(food)
number = [3, 6, 0, 3]
friends.append(number)
print(friends)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
Self-test runs successfully, save and submit
NP28 Password Game
Description
Niu Niu and Niu Mei play a password game together. Niu Niu, as the sender, will send a 4-digit integer to Niu Mei, and Niu Mei will crack the password after receiving it.
The cracking scheme is as follows: each digit must be added3
Divide by9
replace the digit with the remainder of1
bit and number3
Bit number swap, p.2
bit and number4
Bit number exchange.
Please output the password that Niumei cracked.
Enter Description:
Enter a four-digit integer.
Output description:
Output the cracked password in the form of four digits.
Example 1
input:1234
output:6745
Note:
The input will not have leading 0s, but the output must keep leading 0s
Code:
num = input()
ls = []
for i in num:
ls.append((int(i)+3)%9)
print(f"{ls[2]}{ls[3]}{ls[0]}{ls[1]}")
- 1
- 2
- 3
- 4
- 5
Self-test runs successfully, save and submit
NP29 implements stack with list
Description
The stack is a first-in, last-out data structure, similar to squeezing an elevator in our lives. The last entry must be the first out. Now we usePython
list to simulate a stack. Suppose the initial list isstack = [1, 2, 3, 4, 5]
, treat it as a stack, usepop
The function pops the last two elements, and then usesappend
The function adds the input elements to the stack and outputs the entire list after each operation.
Input description:
Enter the integer to be added to the stack.
Output description:
The first line outputs the list after the first pop;
the second line outputs the list after the second pop;
the third line outputs the list after elements are pushed onto the stack.
Example 1
input:1
output:[1, 2, 3, 4]
[1, 2, 3]
[1, 2, 3, 1]
illustrate:
Pop the last element for the first time5
, the second pops the end element4
, adding new elements for the third time1
Code:
stack = [1, 2, 3, 4, 5]
stack.pop(-1)
print(stack)
stack.pop(-1)
print(stack)
num = eval(input())
stack.append(num)
print(stack)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
Self-test runs successfully, save and submit
NP30 implements a queue with a list
Description
Queue is a first-in, first-out data structure, similar to queuing up for meals in a canteen. Of course, the elements that enter the queue first must leave the queue first.Python
List mock queues. existing listqueue = [1, 2, 3, 4, 5]
is treated as a queue, please usepop
The function takes out the first element of the queue twice in a row, and then usesappend
The function adds input elements to the end of the queue, and outputs the full list after each operation.
Input description:
Enter an integer representing the element to add to the queue.
Output description:
The first line outputs the list after the first removal of the queue head;
the second line outputs the list after the second removal of the queue head;
the third line outputs the list after adding elements to the queue.
Example 1
input:8
output:[2, 3, 4, 5]
[3, 4, 5]
[3, 4, 5, 8]
illustrate:
The first pops the first element 1 of the queue, the second pops the first element 2, and the third adds the number 8 to the end of the queue
Code:
queue = [1, 2, 3, 4, 5]
queue.pop(0)
print(queue)
queue.pop(0)
print(queue)
num = eval(input())
queue.append(num)
print(queue)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
Self-test runs successfully, save and submit
NP31 team grouping
Description
Create a listgroup_list
, which in turn contains the string'Tom', 'Allen', 'Jane', 'William', 'Tony'
Indicates the names of members of this group. There are currently three tasks that they need to complete. According to the complexity of different tasks and the actual situation, they need to send 2 people, 3 people, and 2 people to complete them. They decide to allocate tasks by slicing the list.
useprint()
statement and slice to print a listgroup_list
The first two elements represent the name of the person who did the first task, and then useprint()
statement and slice to print a listgroup_list
The middle three elements represent the name of the person doing the second task, and then useprint()
statement and slice to print a listgroup_list
The last two elements represent the name of the person who did the third task.
Input description:
None
Output description:
output:
['Tom', 'Allen']
['Allen', 'Jane', 'William']
['William', 'Tony']
Code:
group_list = ['Tom', 'Allen', 'Jane', 'William', 'Tony']
print(group_list[0:2])
print(group_list[1:4])
print(group_list[3:5])
- 1
- 2
- 3
- 4
Self-test runs successfully
save commit
Recommended: Niu Ke Question Ba - classic high frequency interview question bank
🌐
Job Search Artifact-|Written Exam Question Bank|Interview Experience|Dachang Interview Questions
👉Click the link to register and study