How raw_input () works. Strip (). Split () in Python in this code?

I hope the community can explain this to me better. Below is the goal, I am trying to understand this code with the goal in mind.

Purpose: Initialize your list and read the value, followed by lines of commands, each of which will have the types listed above. Go through each command in order and perform the corresponding operation on your list.

Input Example:

12
insert 0 5
insert 1 10
etc.

Output Example:

[5, 10]
etc.

The first line contains an integer n, indicating the number of commands. Each line of the following lines contains one of the commands described above.

The code:

n = int(raw_input().strip())

List = []
for number in range(n):
args = raw_input().strip().split(" ")
if args[0] == "append":
    List.append(int(args[1]))
elif args[0] == "insert":
    List.insert(int(args[1]), int(args[2]))

So this is my interpretation of the variable "args". You take the original input from the user, then remove the spaces from the original input. Once this is removed, the split function places the string in the list.

" 0 5", strip() "insert05"?

+4
2

python split(delimiter) , , ( - ), strip()

, :

raw_input()          #' insert 0 5     '
raw_input().strip()  #'insert 0 5'
raw_input().strip().split()  #['insert', '0', '5']

split(';') , , , 0; 5 '

+7

, .remove(" "), .strip() .

0
source

Source: https://habr.com/ru/post/1660729/


All Articles