Get the most significant digit in python

Say I have a list [34523, 55, 65, 2]

What is the most effective way to get [3,5,6,2]which are the most significant numbers. If possible, without changing each of them to str()?

+4
source share
2 answers

Assuming that you are dealing only with positive numbers, you can divide each number by the greatest power 10 less than the number, and then take the word of result.

>>> from math import log10, floor
>>> lst = [34523, 55, 65, 2]
>>> [floor(x / (10**floor(log10(x)))) for x in lst]
[3, 5, 6, 2]

If you use Python 3 instead of overtaking the result, you can use the integer division operator //:

>>> [x // (10**floor(log10(x))) for x in lst]
[3, 5, 6, 2]

, , . ( , , 0 1.)

>>> [int(str(x)[0]) for x in lst]
[3, 5, 6, 2]

, , . , , .

+13

, python 3.6.1:

from timeit import timeit

from math import *


lst = list(range(1, 10_000_000))


# 3.6043569352230804 seconds
def most_significant_str(i):
    return int(str(i)[0])


# 3.7258850016013865 seconds
def most_significant_while_floordiv(i):
    while i >= 10:
        i //= 10
    return i


# 4.515933519736952 seconds
def most_significant_times_floordiv(i):
    n = 10
    while i > n:
        n *= 10
    return i // (n//10)


# 4.661690454738387 seconds
def most_significant_log10_floordiv(i):
    return i // (10 ** (log10(i) // 1))


# 4.961193803243334 seconds
def most_significant_int_log(i):
    return i // (10 ** int(log10(i)))


# 5.722346990002692 seconds
def most_significant_floor_log10(i):
    return i // (10 ** floor(log10(i)))


for f in (
    'most_significant_str',
    'most_significant_while_floordiv',
    'most_significant_times_floordiv',
    'most_significant_log10_floordiv',
    'most_significant_int_log',
    'most_significant_floor_log10',
):
    print(
        f,
        timeit(
            f"""
for i in lst:
    {f}(i)
            """,
            globals=globals(),
            number=1,
        ),
    )

, range(1, 10_000_000), int(str(i)[0]) , . , , while:

def most_significant_while_floordiv(i):
    while i >= 10:
        i //= 10
    return i
+1

Source: https://habr.com/ru/post/1617670/


All Articles