+ 1
[Py] How to input a list into sololearn's compiler ?
say I have a code that deals with lists and I want to feed a list to the input() function, is there any way to achieve this ? I tried with commas and spaces but it didn't work edit : my code now works ! https://code.sololearn.com/cLPf8NvJ2z6z/?ref=app
3 Respostas
+ 4
The input() always returns a string, so do the following:
st = input()
# expect separation with spaces
lis = st.split(“ “)
# lis is a list of strings
nums = [float(n) for n in lis]
# nums is a list of floats
+ 2
Yup, there is a rather simple solution:
a = input().split()
'a' is now an array with each word (string separated by spaces) of the input as a separate value.
Alternatively you could try something like this:
a = input().split(', ')
This splits the input at every comma and space rather than spaces.
+ 1
neat, thank you :)
(of course it would have to do with list methods ^^)