获取 python 中的最高有效数字

Get most significant digit in python

假设我有列表 [34523, 55, 65, 2]

获得 [3,5,6,2] 最有效数字的最有效方法是什么。如果可能,不更改每个更改为 str()?

假设您只处理正数,您可以将每个数字除以小于该数字的 10 的最大次方,然后对结果取底。

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

如果您使用 Python 3,您可以使用整数除法运算符 //:

而不是对结果进行取整
>>> [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]

如果这是一段性能关键的代码,您应该衡量这两个选项,看看哪个更快。如果它不在性能关键的代码段中,请使用对您来说最易读的代码。

我使用 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