0
How did I get a list as input?
12 ответов
+ 2
mylist=list(input())
mylist is a list with every character in the string inputted being an element of the list.
+ 1
input() only returns a string. You can split it however using the split() function
list_input = input().split(', ')
This will split it by commas
+ 1
input_list = eval(input())
if you type in a standard python list then input_list will really be a list
for example [1, 2, 3, 4]
just type this in and get the list
eval() is much more than this:
print(eval(“1*2*3*4”)) #24
print(eval(“12”)) #12
eval(str) executes the string as some code and returns what the string returns
exec(str) executes the string as some code and returns None
+ 1
Cbr✔[ Most active ] We can use some regular expressions to check if the user input is a list and then convert it. This may prevent dangerous code from being executed.
+ 1
Cbr✔[ Most active ] We can first store the string in a variable and then check if it is a list, if yes then execute it. If we just use list(), what if we want a list of integers, floats, booleans or a list of lists?
0
Cbr✔[ Most active ] input() returns string
float() takes a string amd returns a float
float(input()) thus returns a float because the string gets passed into float()
0
Cbr✔[ Most active ] then simply list(input()) will work, and .split('')