How to split integer python

Split Integer into Digits in Python

While using list comprehension in the above code block, we converted the given number to a string using the str() method. Then, we used list comprehension to iterate over the converted string..

We used int() in each iteration to convert the sub-string to an integer. All this code ( int(i) for i in str(number) ) is enclosed in [] to get list-type results.

Using for Loop

To split number into digits in Python:

  • Use the str() to transform the specified integer to a string.
  • Use the for loop to iterate over the converted string.
  • Use int() to convert every substring to an integer in each iteration. Then, pass the converted integer to the append() method to append elements to a list.

First, we used str() and passed the specified number to this function to convert from integer to string. Then, we used a for loop to iterate over the converted string.

In each iteration, we first converted the sub-string to an integer and passed it to the append() method, which is used to append items to a list named list_of_digits .

Читайте также:  Python add item to dict python

Further reading:

Extract Integer from String in Python
Python find number in String

Using map() with list() & str.split()

To split the given number into digits in Python:

  • Use str() to convert the specified number to string to make it iterable.
  • Use map() to run a particular function for each substring in the iterable (we created in the previous step).
  • Use list() to create a list object.

After executing the above code successfully, we will get the following output on the console.

For this code example, we used the str() method, which took the number as a parameter and converted it to a string. Why did we need to convert it? Because we want to make it iterable to use the map() function.

The map() took two parameters, the first parameter is the int function that we wanted to execute, and the second is the iterable returned by str(number) . Here, the map() function converted each element (which is sub-string here) of an iterable to an integer.

After that, we used the list() function to create a list object. Here, the list object is a collection that is ordered and changeable.

Suppose we are given a number in string format already; how can we convert them to digits? In that case, we can do as follows:

  • Use str.split() to split the given string.
  • Use the map() function to iterate over each list element and convert it to an integer.
  • Use the list() function to convert it to a list object.

Источник

Split Integer Into Digits in Python

Split Integer Into Digits in Python

  1. Use List Comprehension to Split an Integer Into Digits in Python
  2. Use the math.ceil() and math.log() Functions to Split an Integer Into Digits in Python
  3. Use the map() and str.split() Functions to Split an Integer Into Digits in Python
  4. Use a for Loop to Split an Integer Into Digits in Python

This tutorial will discuss different methods to split an integer into digits in Python.

Use List Comprehension to Split an Integer Into Digits in Python

List comprehension is a much shorter and graceful way to create lists that are to be formed based on given values of an already existing list.

In this method, str() and int() functions are also used along with List comprehension to split the integer into digits. str() and int() functions are used to convert a number to a string and then to an integer respectively.

The following code uses list comprehension to split an integer into digits in Python.

num = 13579 x = [int(a) for a in str(num)] print(x) 

The number num is first converted into a string using str() in the above code. Then, list comprehension is used, which breaks the string into discrete digits. Finally, the digits are converted back to an integer using the int() function.

Use the math.ceil() and math.log() Functions to Split an Integer Into Digits in Python

The operation of splitting the integer into digits in Python can be performed without converting the number to string first. Moreover, this method is about twice as fast as converting it to a string first.

The math.ceil() function rounds off a number up to an integer. The math.log() function provides the natural logarithm of a number. To use both these functions, we should import the math library.

The math module can be defined as an always accessible and standard module in Python. It provides access to the fundamental C library functions.

The following code uses list comprehension, math.ceil() and math.log() functions to split an integer into digits in Python.

import math n = 13579 x = [(n//(10**i))%10 for i in range(math.ceil(math.log(n, 10))-1, -1, -1)] print(x) 

Источник

How to split integer python

Last updated: Feb 19, 2023
Reading time · 4 min

banner

# Table of Contents

# Split an integer into digits in Python

To split an integer into digits:

  1. Use the str() class to convert the integer to a string.
  2. Use a list comprehension to iterate over the string.
  3. On each iteration, use the int() class to convert each substring to an integer.
Copied!
an_int = 13579 list_of_digits = [int(x) for x in str(an_int)] print(list_of_digits) # 👉️ [1, 3, 5, 7, 9]

split integer into digits

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.

# Split an integer into digits using a for loop

This is a three-step process:

  1. Use the str() class to convert the integer to a string.
  2. Use a for loop to iterate over the string.
  3. Use the int() class to convert each substring to an integer and append them to a list.
Copied!
an_int = 13579 list_of_digits = [] for x in str(an_int): list_of_digits.append(int(x)) print(list_of_digits) # 👉️ [1, 3, 5, 7, 9]

split integer into digits using for loop

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.

# Split an integer into digits using map()

This is a three-step process:

  1. Use the str() class to convert the integer to a string.
  2. Pass the int class and the string to the map() function.
  3. Use the list() class to convert the map object to a list.
Copied!
an_int = 13579 list_of_digits = list(map(int, str(an_int))) print(list_of_digits) # 👉️ [1, 3, 5, 7, 9]

split integer into digits using map

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

Copied!
an_int = 13579 a_str = str(an_int) list_of_digits = list(map(int, a_str)) print(list_of_digits) # 👉️ [1, 3, 5, 7, 9]

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.

# Split an integer into digits using math.ceil() and math.log()

You can also use the math.ceil() and math.log() methods if you need to split the integer into digits without converting to a string.

Copied!
import math an_int = 13579 x = math.log(an_int, 10) y = math.ceil(x) list_of_digits = [(an_int//(10**i)) % 10 for i in range(y, -1, -1) ][bool(math.log(an_int, 10) % 1):] print(list_of_digits) # [1, 3, 5, 7, 9]

The math.ceil method returns the smallest integer greater than or equal to the provided number.

Copied!
import math result_1 = math.ceil(25 / 4) print(result_1) # 👉️ 7 result_2 = 25 / 4 print(result_2) # 👉️ 6.25

The math.log() method returns the natural logarithm of a number.

You can also extract the logic into a reusable function.

Copied!
import math def split_integer(an_int): x = math.log(an_int, 10) y = math.ceil(x) list_of_digits = [(an_int//(10**i)) % 10 for i in range(y, -1, -1) ][bool(math.log(an_int, 10) % 1):] return list_of_digits print(split_integer(12345)) # [1, 2, 3, 4, 5] print(split_integer(100)) # [1, 0, 0] print(split_integer(563)) # [5, 6, 3]

The split integer function takes an integer as a parameter and splits the integer into a list of digits.

The solution is quite difficult to read, however, it is a bit faster because it doesn’t require us to convert the value to a string.

# Split an integer into digits using divmod

If you need to split an integer into digits from right to left, use the divmod method.

Copied!
an_int = 13579 list_of_digits = [] while an_int > 0: an_int, remainder = divmod(an_int, 10) list_of_digits.append(remainder) print(list_of_digits) # [9, 7, 5, 3, 1]

split integer into digits using divmod

Notice that the result is produced from right to left.

The divmod function takes two numbers and returns a tuple containing 2 values:

  1. The result of dividing the first argument by the second.
  2. The remainder of dividing the first argument by the second.

On each iteration of the while loop, we use the divmod() function to get the result and the remainder of the division.

The remainder gets pushed into the list until the integer is equal to or less than 0 .

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.

Источник

Оцените статью