1

I'm using raw_input for the user to type in an integer, for example '123456'.

How do I convert the string number by number so I can put them in a list: [1,2,3,4,5]?

Is the concept the same for two numbers like 12,34,56?

Alex Riley
  • 169,130
  • 45
  • 262
  • 238
John
  • 33
  • 4
  • To get numbers with two or more digits, you can refer to this Q: http://stackoverflow.com/questions/4628290/pairs-from-single-list – Jasper Dec 20 '14 at 21:29
  • 1
    If you use raw_input to collect input, nothing prevents a user from entering invalid numbers so you also need to do error handling. This could be as simple as ignoring non-numeric digits. Be sure to include and an input (i.e., just hitting return) in your testing. – Gary Walker Dec 20 '14 at 21:29

3 Answers3

0

If you want to transform the string you read in from raw_input to a list of int...

number = raw_input("Number? ")
li = [int(i) for i in number]

There's only a little bit more work that you have to do to expand it to multiple digits, but I leave that as an exercise for the reader.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • How does [int(i) for i in number] exactly work? Is this a shortening of a longer code? Thx – John Dec 20 '14 at 22:00
0

if user enter the numbers Consecutive you you can use the following list comprehension :

>>> num=raw_input ("enter the numbers : ")
enter the numbers : 123459
>>> l=[int(i) for i in num]
>>> l
[1, 2, 3, 4, 5, 9]

and for numbers with len 2 that uses , as separator you need to split the input string then convert it to int :

>>> num=raw_input ("enter the numbers : ")
enter the numbers : 12,34,56
>>> l=[int(i) for i in num.split(',')]
>>> l
[12, 34, 56]

But there is another way , if you dont want to convert the numbers you can put your raw_input function inside a loop :

>>> l=[]
>>> for i in range (5):
...  l.append(int(raw_input('enter %dth number :'%i)))
... 
enter 0th number :1
enter 1th number :5
enter 2th number :12
enter 3th number :56
enter 4th number :2345
>>> l
[1, 5, 12, 56, 2345]

and if you want to get number with len 2 from one input you can use slicing :

>>> num=raw_input ("enter the numbers : ")
enter the numbers : 1234424
>>> [int(num[i:i+2]) for i in range(0,len(num),2)]
[12, 34, 42, 4]
Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • Nice solution. In the second case I was actually looking for a way to get [12,34,56] when a user use the same input as the example before(123456). – John Dec 20 '14 at 22:03
  • actually as a general formula `[num[i:i+n] for i in range(0,len(num),n)` split the string with `n` step ! read more http://www.pythoncentral.io/how-to-slice-listsarrays-and-tuples-in-python/ – Mazdak Dec 20 '14 at 22:17
  • Thanks man! One more thing, how about the first example code [int(i) for i in number]? Is this a general formula or a short version?Source? – John Dec 20 '14 at 22:24
  • @John you're welcome , its list comprehension that make a loop trough your string and convert the elements in each loop to `int` , read more https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions – Mazdak Dec 20 '14 at 22:27
0
s = '123456'
print(map(int,s))

s = '12,34,56'
print(map(int,s.split(",")))

[1, 2, 3, 4, 5, 6]
[12, 34, 56]


s = '123456'
print([int("".join(tup)) for tup in zip(s[::2],s[1::2])]

[12, 34, 56]
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321