Here's a way to do it without turning it into a string first [based on some rudimentary benchmarking, this is about twice as fast as stringifying n
first]:
>>> n = 43365644
>>> [[n//[10**i]]%10 for i in range[math.ceil[math.log[n, 10]]-1, -1, -1]]
[4, 3, 3, 6, 5, 6, 4, 4]
Updating this after many years in response to comments of this not working for powers of 10:
[[n//[10**i]]%10 for i in range[math.ceil[math.log[n, 10]], -1, -1]][bool[math.log[n,10]%1]:]
The issue is that with powers of 10 [and ONLY with these], an extra step is required. ---So we use the remainder in the log_10 to determine whether to remove the leading 0
--- We can't
exactly use this because floating-point math errors cause this to fail for some powers of 10. So I've decided to cross the unholy river into sin and call upon regex.
In [32]: n = 43
In [33]: [[n//[10**i]]%10 for i in range[math.ceil[math.log[n, 10]], -1, -1]][not[re.match['10*', str[n]]]:]
Out[33]: [4, 3]
In [34]: n = 1000
In [35]: [[n//[10**i]]%10 for i in range[math.ceil[math.log[n, 10]], -1, -1]][not[re.match['10*', str[n]]]:]
Out[35]: [1, 0, 0, 0]
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
The interconversion of data types is a problem that is quite common in programming. Sometimes we need to convert a single number to list of integers and we don’t wish to spend several lines of code doing it. Hence having ways to perform this task using shorthands comes in handy. Let’s discuss ways in which this can be performed.
Method #1: Using list comprehension
Itcan be used as a shorthand for the longer format of the naive method. In this method, we convert the number to a string and then extract each character and re-convert it to an integer.
Python3
num
=
2019
print
[
"The original number is "
+
str
[num]]
res
=
[
int
[x]
for
x
in
str
[num]]
print
[
"The list from number is "
+
str
[res]]
Output
The original number is 2019 The list from number is [2, 0, 1, 9]
Method #2: Using map[] map function can be used to perform the following task converting each of the string converted numbers to the desired integer value to be reconverted to the list format.
Python3
num
=
2019
print
[
"The original number is "
+
str
[num]]
res
=
list
[
map
[
int
,
str
[num]]]
print
[
"The list from number is "
+
str
[res]]
Output
The original number is 2019 The list from number is [2, 0, 1, 9]
Method #3: Using enumerate function
Python3
n
=
2019
res
=
[
int
[x]
for
a,x
in
enumerate
[
str
[n]]]
print
[res]
Method: Using lambda function
Python3
n
=
2019
x
=
list
[
filter
[
lambda
i:[i],
str
[
2019
]]]
print
[x]
Output
['2', '0', '1', '9']
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
The problem of finding the summation of digits of numbers is quite common. This can sometimes come in form of a list and we need to perform that.
This has application in many domains such as school programming and web development. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using loop + str[]
This is brute force method to perform this particular task. In this, we run a loop for each element, convert each digit to string, and perform the count of the sum of each digit.
Python3
test_list
=
[
12
,
67
,
98
,
34
]
print
[
"The original list is : "
+
str
[test_list]]
res
=
[]
for
ele
in
test_list:
sum
=
0
for
digit
in
str
[ele]:
sum
+
=
int
[digit]
res.append[
sum
]
print
[
"List Integer Summation : "
+
str
[res]]
Output :
The original list is : [12, 67, 98, 34] List Integer Summation : [3, 13, 17, 7]
Method #2 : Using sum[] + list comprehension
This task can also be performed using shorthand using above functionalities. The sum[] is used to compute summation and list comprehension is used to compute iterations.
Python3
test_list
=
[
12
,
67
,
98
,
34
]
print
[
"The original list is : "
+
str
[test_list]]
res
=
list
[
map
[
lambda
ele:
sum
[
int
[sub]
for
sub
in
str
[ele]], test_list]]
print
[
"List Integer Summation : "
+
str
[res]]
Output :
The original list is : [12, 67, 98, 34] List Integer Summation : [3, 13, 17, 7]
Method #3 : Using sum[] + reduce[]
This task can also be performed using shorthand using the above functionalities. The sum[] is used to compute summation and reduce function from functools module.
Python3
from
functools
import
reduce
test_list
=
[
12
,
67
,
98
,
34
]
print
[
"The original list is : "
+
str
[test_list]]
res
=
[
reduce
[
lambda
x, y:
int
[x]
+
int
[y],
list
[
str
[i]]]
for
i
in
test_list]
print
[
"List Integer Summation : "
+
str
[res]]
Output:
The original list is : [12, 67, 98, 34] List Integer Summation : [3, 13, 17, 7]