1

Possible Duplicate:
How can I get a list as input from the user in Python?

I currently have this:

c = eval(input("Enter a group of numbers "))
#say someone types 123
print (c)
#prints out 123

I want this:

c = eval(input("Enter a group of numbers "))
#say they enter 123
print (c)
#prints out [1,2,3]

I want 123 to end up as [1,2,3]. How can I do that?

Community
  • 1
  • 1
Average kid
  • 6,885
  • 6
  • 21
  • 17
  • 2
    Do NOT use `eval`. Also, if you're using python 2, don't use `input`. Both will result in an arbitrary code execution vulnerability. – Colin Dunklau Oct 03 '12 at 17:37
  • I know everyone likes easy rep, but for quality Q&A, common questions really need to be closed, not answered with a dozen identical answers every time. – Junuxx Oct 03 '12 at 17:41

7 Answers7

6
In [32]: c=raw_input()
123

In [33]: map(int,c)
Out[33]: [1, 2, 3]

use split() if the input is something like 1 2 3:

In [37]: c=raw_input()
1 2 3

In [38]: map(int,c.split())
Out[38]: [1, 2, 3]
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
4

You can convert the numbers into ints using map():

>>> map(int, '123')
[1, 2, 3]
Blender
  • 289,723
  • 53
  • 439
  • 496
4
>>> s = '123'
>>> [int(c) for c in s]
[1, 2, 3]
John Vinyard
  • 12,997
  • 3
  • 30
  • 43
1

How about?:

c = [int(x) for x in input("Enter a group of numbers ")]
#list comprehension over the input function

Entering 123 the result is [1, 2, 3]

OK, lets say that for python 2.x (input returns an int object)

c = [int(x) for x in str(input("Enter a group of numbers "))]
#added an str() function for iterating
Netwave
  • 40,134
  • 6
  • 50
  • 93
0

You could convert it to a string, and then convert each character in the string into a number.


myStr = str(myInt)
out = [int(i) for i in myStr]

BostonJohn
  • 2,631
  • 2
  • 26
  • 48
0

First of all, the following:

c = eval(input("Enter a group of numbers "))

Is the same as:

c = eval(eval(raw_input("Enter a group of numbers ")))

So you are calling eval twice now. More information on input can be found here.

This is a possible solution for what you want:

c = raw_input("Enter a group of numbers "))
c = [int(i) for i in c]
print(c)

You can of course reduce the above example to two lines (even one actually).

Derecho
  • 161
  • 4
0

You should not use eval on user input generally. Someone can type a statement that will eval into mischief.

For the same reason, you should avoid the use of input() since it is the equivalent of eval(raw_input()) also can lead to mischief -- intended or not.

You can, however, SAFELY get Python interpretation of user input into Python data structures with ast.literal_eval:

>>> import ast
>>> ast.literal_eval(raw_input('Type Python input: '))
Type Python input: 1,2,3
(1, 2, 3)
>>> ast.literal_eval(raw_input('Type Python input: '))
Type Python input: [1,2,3]
[1, 2, 3]
>>> ast.literal_eval(raw_input('Type Python input: '))
Type Python input: 123
123
>>> ast.literal_eval(raw_input('type a number: '))
type a number: 0xab
171

(In each case, the first line after >>> Type Python input: is what I typed into the raw_input()

If you want to split digits apart, you can do this:

>>> [int(c) for c in raw_input() if c in '1234567890']
1234
[1, 2, 3, 4]
>>> [int(c) for c in raw_input() if c in '1234567890']
123a45
[1, 2, 3, 4, 5]

Notice the non digit is filtered.

the wolf
  • 34,510
  • 13
  • 53
  • 71