Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a number to a list of integers

How do I write the magic function below?

>>> num = 123 >>> lst = magic(num) >>> >>> print lst, type(lst) [1, 2, 3], <type 'list'> 
like image 942
user94774 Avatar asked Apr 23 '09 05:04

user94774


People also ask

How do you convert numbers to integers?

To convert from double to integer, use the as. integer() method and to check the integer value, use the is. integer() method.

How do you turn a set of numbers into a list?

Approach #1 : Using list(set_name) . Typecasting to list can be done by simply using list(set_name) . Using sorted() function will convert the set into list in a defined order. The only drawback of this method is that the elements of the set need to be sortable.

How do you turn a string of numbers into a list of integers?

To convert each list item from a string to an integer, use the map function. The map function takes two arguments: A function. In this case the function will be the int function.


2 Answers

You mean this?

num = 1234 lst = [int(i) for i in str(num)] 
like image 192
Bjorn Avatar answered Sep 19 '22 10:09

Bjorn


a = 123456 b = str(a) c = []  for digit in b:     c.append (int(digit))  print c 
like image 22
RedBlueThing Avatar answered Sep 17 '22 10:09

RedBlueThing