Split an integer into digits in Python #
To split an integer into digits:
- Use the
str[]
class to convert the integer to a string. - Use a list comprehension to iterate over the string.
- On each iteration, use the
int[]
class to convert each substring to an integer.
Copied!
my_int = 13579 my_list = [int[x] for x in str[my_int]] print[my_list] # 👉️ [1, 3, 5, 7, 9]
We used the str[]
class to convert the integer to a string, so we can iterate over the string.
The next step is to use a list comprehension to iterate over the string.
List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.
On each iteration, we pass the string to the int[]
class to convert it to an integer.
You can also use a simple
for
loop to achieve the same result.
To split an integer into digits:
- Use the
str[]
class to convert the integer to a string. - Use a
for
loop to iterate over the string. - Use the
int[]
class to convert each substring to an integer and append them to a list.
Copied!
my_int = 13579 my_list = [] for x in str[my_int]: my_list.append[int[x]] print[my_list] # 👉️ [1, 3, 5, 7, 9]
We iterate over the digits that are wrapped in a string and on each iteration, we use the int[]
class to convert the value to an integer before
appending the result to a list.
Alternatively, you can use the map[]
function to split an integer into digits.
Copied!
my_int = 13579 my_list = list[map[int, str[my_int]]] print[my_list] # 👉️ [1, 3, 5, 7, 9]
The map[] function takes a function and an iterable as arguments and calls the function with each item of the iterable.
Strings are iterable and integers are not, so the first step is to convert the integer to a string.
The int[]
class gets passed each substring from the string and converts the values to integers.
Note that the map[]
function returns a map
object [not a list], so we have to use the list[]
class to convert the map
object to a list.
I'd rather not turn an integer into a string, so here's the function I use for this:
def digitize[n, base=10]:
if n == 0:
yield 0
while n:
n, d = divmod[n, base]
yield d
Examples:
tuple[digitize[123456789]] == [9, 8, 7, 6, 5, 4, 3, 2, 1]
tuple[digitize[0b1101110, 2]] == [0, 1, 1, 1, 0, 1, 1]
tuple[digitize[0x123456789ABCDEF, 16]] == [15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
As you can see, this will yield digits from right to left. If you'd like the digits from left to right, you'll need to create a sequence out of it, then reverse it:
reversed[tuple[digitize[x]]]
You can also use this function for base conversion as you split the integer. The following example splits a hexadecimal number into binary nibbles as tuples:
import itertools as it
tuple[it.zip_longest[*[digitize[0x123456789ABCDEF, 2]]*4, fillvalue=0]] == [[1, 1, 1, 1], [0, 1, 1, 1], [1, 0, 1, 1], [0, 0, 1, 1], [1, 1, 0, 1], [0, 1, 0, 1], [1, 0, 0, 1], [0, 0, 0, 1], [1, 1, 1, 0], [0, 1, 1, 0], [1, 0, 1, 0], [0, 0, 1, 0], [1, 1, 0, 0], [0, 1, 0, 0], [1, 0, 0, 0]]
Note that this method doesn't handle decimals, but could be adapted to.
Hello guys. How are you all? I hope you all fine. In this tutorial we will learn about How to split Integer Into Digits in Python. so without wasting time lets learn about of this. to split Integer Into Digits in Python just Use math.ceil[].By using
math.ceil[] you can split Integer Into Digits in Python. Lets learn about of this by given below example: to split Integer Into Digits in Python just Use List comprehension.By using List comprehension you can split Integer Into Digits in Python. Lets learn about of this by given below example: To split Integer Into Digits in Python just Use List comprehension.By using List comprehension you can split Integer Into Digits in Python. Lets learn about of this by given below example: import math num = 8798795 result = [[num//[10**i]]%10 for i in range[math.ceil[math.log[num, 10]]-1, -1, -1]] print[result]
Output : [8, 7, 9, 8, 7, 9, 5]
num = 8798795 result = [int[a] for a in str[num]] print[result]
Output : [8, 7, 9, 8, 7, 9, 5]
num = 8798795 result = [int[a] for a in str[num]] print[result]
Output : [8, 7, 9, 8, 7, 9, 5]
Method 1: Use math.ceil[]
By using math.ceil[] you can split Integer Into Digits. Lets learn about of this by given below example:
import math
num = 8798795
result = [[num//[10**i]]%10 for i in range[math.ceil[math.log[num, 10]]-1, -1, -1]]
print[result]
Output :
[8, 7, 9, 8, 7, 9, 5]
Method 2: Use List comprehension
By using List comprehension you can split Integer. Lets learn about of this by given below example:
num = 8798795
result = [int[a] for a in str[num]]
print[result]
Output :
[8, 7, 9, 8, 7, 9, 5]
Method 3: Use for loop
By using for loop you can split Integer Into Digits. Lets learn about of this by given below example:
num = '8798795'
x = 1
result = []
for i in range[0, len[num], x]:
result.append[int[num[i : i + x]]]
print["The list : " + str[result]]
Output :
The list : [8, 7, 9, 8, 7, 9, 5]
Method 4: Use int[] and slice
By using int[] and slice you can split Integer. Lets learn about of this by given below example:
mystr = '8798795'
x = 1
res = []
for idx in range[0, len[mystr], x]:
res.append[int[mystr[idx : idx + x]]]
print["The list : " + str[res]]
Output :
The list : [8, 7, 9, 8, 7, 9, 5]
Conclusion
It’s all About this Tutorial. Hope all methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you?
Also, Read
- How to comment a Block of Code in Python