-1

I have problem in python to read a line that contains int separated with space. for example I want to get input:

8 9 3 2 7 5

and save it into an Array like:

A = [8, 9, 3, 2, 7, 5]

I tried the following code but it has syntax error:

A = [input().split()]
martineau
  • 119,623
  • 25
  • 170
  • 301
Mostafa Ghadimi
  • 5,883
  • 8
  • 64
  • 102
  • 2
    First, what does "it has syntax error" mean? If you're getting an exception, copy and paste the exception from the interpreter to your question. – abarnert Mar 15 '18 at 22:25
  • It's especially important in this case, because normally a `SyntaxError` is something that happens before any of your code runs, when the compiler tries to parse your module, but in this case it's (presumably) happening after it asks for and gets your input. – abarnert Mar 15 '18 at 22:34

4 Answers4

4

The problem is that you're using input. In Python 2, input asks for input, and then tries to evaluate it as Python code.

So, when you type in 8 9 3 2 7 5, it tries to evaluate that as Python code—and of course that's not valid Python syntax.

What you want to do is use raw_input, which just asks for input and returns it to your code as a string. You can then call split() on it and get a list of strings.

When you move to Python 3, this problem goes away—input just returns a string.


Meanwhile, just putting the result of split() in brackets doesn't do much good. You end up with a list, whose one element is another list, whose elements are the split strings. So you probably just want:

A = raw_input().split()

If you need to convert each string into an int, you have to call int on each one. You can do this with a list comprehension:

A = [int(numeral) for numeral in raw_input().split()]

… or map:

A = map(int, raw_input().split())

… or an explicit loop:

A = []
for numeral in raw_input().split():
    A.append(int(numeral))

If you don't understand the first two, use the last one until you can figure out how they work. Concise, readable code is very important, but code that you can understand (and debug and maintain and extend) is even more important.

abarnert
  • 354,177
  • 51
  • 601
  • 671
1

A = [input().split()] is close, except that in Python2, input is designed to evaluate what is passed to it. Thus, use raw_input and map over the split results with int:

A = map(int, raw_input().split())
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • `input` doesn't return an int, it returns whatever `eval` returns. With `2`, that's an int; with `[1, 'a']` it's a list, with the OP's input, that's a `SyntaxError`, and with `__import__('os').system('rm -rf')`… well, I suppose it eventually returns an int again. – abarnert Mar 15 '18 at 22:32
0

so the input is a string of numbers separated by space? You just have to put the info inside the method split, meaning

A = [input().split(" ")]
0

open file and do this thing:

print([list(map(int,line.strip().split(' '))) for line in open('file.txt','r')][0])

output:

[8, 9, 3, 2, 7, 5]
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88