You can do it with integer division and remainder methods
def get_digit(number, n):
return number // 10**n % 10
get_digit(987654321, 0)
# 1
get_digit(987654321, 5)
# 6
The // performs integer division by a power of ten to move the digit to the ones position, then the % gets the remainder after division by 10. Note that the numbering in this scheme uses zero-indexing and starts from the right side of the number.
You can do it with integer division and remainder methods
def get_digit(number, n):
return number // 10**n % 10
get_digit(987654321, 0)
# 1
get_digit(987654321, 5)
# 6
The // performs integer division by a power of ten to move the digit to the ones position, then the % gets the remainder after division by 10. Note that the numbering in this scheme uses zero-indexing and starts from the right side of the number.
First treat the number like a string
number = 9876543210
number = str(number)
Then to get the first digit:
number[0]
The fourth digit:
number[3]
EDIT:
This will return the digit as a character, not as a number. To convert it back use:
int(number[0])
python - How to extract digits from a number from left to right? - Stack Overflow
checking the value of nth digit of a number
You can target specific digits using modulo and floor division.
myNumber = 1234567 digits12 = myNumber / 100000 # 12 digit3 = (myNumber / 10000) % 10 # 3 digit4 = (myNumber / 1000) % 10 # 4
That way you don't need to change data types (like if you changed myNumber to a string, then used slicing to get the digits you want and turning them back into ints)
Edit: sorry, didn't realize you were using Python 2, where / is floor division.
More on reddit.compython - How to find length of digits in an integer? - Stack Overflow
How to split int into digits?
I want to convert 19 into [1, 9]. I tried split but on a '19' but gives me same '19'.
If you don't have problem with recursion approach then here is a solution with little change in your code:-
def get_digit(num):
if num < 10:
print(num)
else:
get_digit(num // 10)
print(num % 10)
Usage
>>> get_digit(543267)
5
4
3
2
6
7
Here is a generator which returns the digits of a positive integer in a left to right manner:
from math import floor, log10
def digits(n):
"""generator which returns digits in left to right order"""
k = floor(log10(n))
for e in range(k,-1,-1):
d,n = divmod(n,10**e)
yield d
For example,
>>> list(digits(2016))
[2, 0, 1, 6]
Hello everyone,
First some background to the problem I am trying to solve. I have a bunch of 7 digit integers and I need to sort them based on the value of the first 4 digits. For example, I need to classify the number if the first 2 digits are between 44 and 59 and the third and fourth digits are between 4 and 7 (inclusive) as area A.
So far I have simply dumped all of my numbers into a list and sorted them. I am thinking that I need to make a definition with some for loops in them but I am not sure how to check the values of the digits of the numbers. Any ideas of how to check these values?
I hope I explained this so that everyone can understand it.
Side note I am using window 8 using python 2.7.6
Thanks in advance for the help
Edit: Just FYI I am very new to Python, I only start learning it in my spare time about a month ago.
You can target specific digits using modulo and floor division.
myNumber = 1234567
digits12 = myNumber / 100000 # 12
digit3 = (myNumber / 10000) % 10 # 3
digit4 = (myNumber / 1000) % 10 # 4
That way you don't need to change data types (like if you changed myNumber to a string, then used slicing to get the digits you want and turning them back into ints)
Edit: sorry, didn't realize you were using Python 2, where / is floor division.
I'm sure there are ways to do this numerically but the easiest way I can think of to check a bunch of nth digit values and possibly iterate through them is to convert the number into a string using the str() function.
Once the number is a string you can access digits using indexing just like you would with a list.
If you want the length of an integer as in the number of digits in the integer, you can always convert it to string like str(133) and find its length like len(str(123)).
Without conversion to string
import math
digits = int(math.log10(n))+1
To also handle zero and negative numbers
import math
if n > 0:
digits = int(math.log10(n))+1
elif n == 0:
digits = 1
else:
digits = int(math.log10(-n))+2 # +1 if you don't count the '-'
You'd probably want to put that in a function :)
Here are some benchmarks. The len(str()) is already behind for even quite small numbers
timeit math.log10(2**8)
1000000 loops, best of 3: 746 ns per loop
timeit len(str(2**8))
1000000 loops, best of 3: 1.1 µs per loop
timeit math.log10(2**100)
1000000 loops, best of 3: 775 ns per loop
timeit len(str(2**100))
100000 loops, best of 3: 3.2 µs per loop
timeit math.log10(2**10000)
1000000 loops, best of 3: 844 ns per loop
timeit len(str(2**10000))
100 loops, best of 3: 10.3 ms per loop