Simply use the split function :
lst = txt.split(" ")
Answer from Itamar on Stack OverflowSimply use the split function :
lst = txt.split(" ")
separator = " "
average = input()
print average.split(separator)
There is a inbuilt method called split() which will get your job done , you can pass a separator variable to that method indicating the character you want to split upon However the default value is space " " itself , averge.split() would also work fine in your case.
You want this - enter N and then take N number of elements.I am considering your input case is just like this
5
2 3 6 6 5
have this in this way in python 3.x (for python 2.x use raw_input() instead if input())
Python 3
n = int(input())
arr = input() # takes the whole line of n numbers
l = list(map(int,arr.split(' '))) # split those numbers with space( becomes ['2','3','6','6','5']) and then map every element into int (becomes [2,3,6,6,5])
Python 2
n = int(raw_input())
arr = raw_input() # takes the whole line of n numbers
l = list(map(int,arr.split(' '))) # split those numbers with space( becomes ['2','3','6','6','5']) and then map every element into int (becomes [2,3,6,6,5])
raw_input is your helper here. From documentation -
If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised.
So your code will basically look like this.
num_array = list()
num = raw_input("Enter how many elements you want:")
print 'Enter numbers in array: '
for i in range(int(num)):
n = raw_input("num :")
num_array.append(int(n))
print 'ARRAY: ',num_array
P.S: I have typed all this free hand. Syntax might be wrong but the methodology is correct. Also one thing to note is that, raw_input does not do any type checking, so you need to be careful...
Converting a single line string to integer array in Python - Stack Overflow
Convert input() to array in python - Stack Overflow
how do you take user input and add it into an array/list with python
Get a list of string as input from user in loop python - Stack Overflow
Dangerous version:
data = input("Enter your list: ")
parsed_data = eval(data)
If you know the input is safe (only you provide it), you can just use eval
l = eval(input("bring it on:"))
a better version is ast.literal_eval
l = ast.literal_eval(input("much safer:"))
still in both cases you need to be sure the input is not getting from an un-trusted source
If you can make your input to be a json you have an even safer/better solution:
#possible input: [[["Zamek", 2], ["Polonez", 7], ["Wojska Polskiego", 12], ["S\\u0142owia\\u0144ska", 17], ["Solidarno\\u015bci", 21]], [["Zamek", 10], ["Polonez", 15], ["Wojska Polskiego", 21], ["S\\u0142owia\\u0144ska", 24], ["Solidarno\\u015bci", 28]], [["Zamek", 17], ["Polonez", 22], ["Wojska Polskiego", 29], ["S\\u0142owia\\u0144ska", 32], ["Solidarno\\u015bci", 36]], [["Zamek", 22], ["Polonez", 30], ["Wojska Polskiego", 37], ["S\\u0142owia\\u0144ska", 40], ["Solidarno\\u015bci", 45]]]
l = json.loads(input("please provide it in json format..."))
names = ["john" , "bill", "randy"]
add_name = input(names()]
I know this isnt the right format but am I on the right track?
Your array has size 0, this is why you can't access any elements (or assign to them). The correct code would be:
array = []
for i in range(4):
array.append(input("Enter string"))
Try this:
for i in range(4):
array.append(input("Enter string >>"))
Since your array doesn't have any values yet, assigning to array[i] would not work.
You can simply split the string.
result=input.split(" ")
or
string = raw_input("Input here:")
def token(string):
start = 0
i = 0
token_list = []
for x in range(0, len(string)):
if " " == string[i:i+1][0]:
token_list.append(string[start:i+1])
#print string[start:i+1]
start = i + 1
i += 1
token_list.append(string[start:i+1])
return token_list
print token(string)
You can modify your function to look like this:
string = input("Input here:")
def token(string):
start, i = 0, 0
token_list = []
for x in range(0, len(string)):
if " " == string[i:i+1]:
token_list.append(string[start:i+1])
start = i + 1
i += 1
token_list.append(string[start:i+1])
return token_list
print token(string)
You need to append only up until i if you don't want to include the trailing space. The second append is necessary because your condition is checking for a space to include the word, but the last word won't have a trailing space, and instead would have an end of line character or null.
You can do this
import ast
list = '["Foo","Bar","Baz","Woo"]'
list = ast.literal_eval(list)
print list
No-package solution
You can use the eval function:
list = eval('["Foo", "Bar", "Baz", "Woo"]')
print (list)
# ['Foo', 'Bar', 'Baz', 'Woo']
Alternative solution
Based on baptx's comment, an alternative way (probably a better one) is to use the ast package:
from ast import literal_eval
list = literal_eval('["Foo", "Bar", "Baz", "Woo"]')
print (list)
# ['Foo', 'Bar', 'Baz', 'Woo']