[Python brush questions] - Python entry 04 list (below)

insert image description here

🤵‍♂️ Profile: @Flyme awei

profile 👨‍💻 About the author:PythonDomain 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

insert image description here


Niu Ke Question Bank " Online Programming - Python "

pythonThe learning still has the basic knowledge + do-it-yourself synchronization. in the case ofpythonIf 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 - NP31insert image description here

List of common operation methods:

methoddescribe
ls.append(x)in the listlsadd an element at the endx
ls.insert(i, x)in the listlsIncrease the element at the ith position ofx
ls.clear()delete listlsall elements
ls.pop(i)will listlsFirstielements are taken and removed fromlsremove the element from
ls.remove(x)the first element that appears in the listxdelete
ls.reverse()will listlsElement inversion in
ls.copy()generate a new list, copylsall 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,useappendfunction willnameadd tofriendsthe first line of .

ifNiumeiI like to eat pbehind, favorite numbers3,WAYO's favorite foodinh, favorite number6,Niu Ke Lefavorite foodpotato, favorite numbers0,Monafavorite foodbeef, favorite numbers3.

Please create a list againfoodRecord friends' favorite foods in turn, and use the created listappendfunction added tofriendsthe second line of;

then create another listnumberRecord your friends' favorite colors in turn, and use the created listappendfunction added tofriendsthe third line.

sofriendsis a two-dimensionallist,useprintThe 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
insert image description here


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 added3Divide by9replace the digit with the remainder of1bit and number3Bit number swap, p.2bit and number4Bit 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
insert image description here


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 usePythonlist to simulate a stack. Suppose the initial list isstack = [1, 2, 3, 4, 5], treat it as a stack, usepopThe function pops the last two elements, and then usesappendThe 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
insert image description here


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.PythonList mock queues. existing listqueue = [1, 2, 3, 4, 5]is treated as a queue, please usepopThe function takes out the first element of the queue twice in a row, and then usesappendThe 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
insert image description here


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_listThe first two elements represent the name of the person who did the first task, and then useprint()statement and slice to print a listgroup_listThe middle three elements represent the name of the person doing the second task, and then useprint()statement and slice to print a listgroup_listThe 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
insert image description here

save commit
insert image description here

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
insert image description here

Tags: [Python brush questions] - Python entry 04 list (below)

Niu Ke brush questions - Python articles python Development language interview study Brush questions

Related: [Python brush questions] - Python entry 04 list (below)