Python 如何获取数字的前两位数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41271299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:43:35  来源:igfitidea点击:

How can I get the first two digits of a number?

python

提问by John28

I want to check the first two digits of a number in Python. Something like this:

我想在 Python 中检查数字的前两位数字。像这样的东西:

for i in range(1000):

    if(first two digits of i == 15):
        print("15")

    elif(first two digits of i == 16):
        print("16")

Is there a command to check the first two digits of a number? I'd like to avoid commands like if(i>149 and i<160):...

有没有命令可以检查数字的前两位数字?我想避免像这样的命令if(i>149 and i<160):...

回答by ettanany

You can convert your number to string and use list slicing like this:

您可以将您的数字转换为字符串并像这样使用列表切片:

int(str(number)[:2])

Output:

输出:

>>> number = 1520
>>> int(str(number)[:2])
15

回答by dom

Both of the previous 2 answers have at least O(n) time complexity and the string conversion has O(n) space complexity too. Here's a solution for constant time and space:

前两个答案都至少有 O(n) 时间复杂度,字符串转换也有 O(n) 空间复杂度。这是恒定时间和空间的解决方案:

num // 10 ** (int(math.log(num, 10)) - 1)

Function:

功能:

import math

def first_n_digits(num, n):
    return num // 10 ** (int(math.log(num, 10)) - n + 1)

Output:

输出:

>>> first_n_digits(123456, 1)
1
>>> first_n_digits(123456, 2)
12
>>> first_n_digits(123456, 3)
123
>>> first_n_digits(123456, 4)
1234
>>> first_n_digits(123456, 5)
12345
>>> first_n_digits(123456, 6)
123456

You will need to add some checks if it's possible that your input number has less digits than you want.

如果您输入的数字可能少于您想要的位数,您将需要添加一些检查。

回答by ChrisFreeman

Comparing the O(n) time solution with the "constant time" O(1) solution provided in other answers goes to show that if the O(n) algorithm is fast enough, nmay have to get very large before it is slower than a slow O(1).

将 O(n) 时间解决方案与其他答案中提供的“恒定时间”O(1) 解决方案进行比较表明,如果 O(n) 算法足够快,则n可能必须变得非常大,然后才能慢于一个缓慢的 O(1)。

The stringsversion is approx. 60% fasterthan the "maths" version for numbers of 20 or fewer digits. They become closer only when then number of digits approaches 200 digits

字符串的版本约。对于20 位或更少位数的数字,比“数学”版本60% 。只有当位数接近200 位时,它们才会变得更接近

# the "maths" version
import math

def first_n_digits1(num, n):
    return num // 10 ** (int(math.log(num, 10)) - n + 1)

%timeit first_n_digits1(34523452452, 2)
1.21 μs ± 75 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%timeit first_n_digits1(34523452452, 8)
1.24 μs ± 47.5 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

# 22 digits
%timeit first_n_digits1(3423234239472523452452, 2)
1.33 μs ± 59.4 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%timeit first_n_digits1(3423234239472523452452, 15)
1.23 μs ± 61.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

# 196 digits
%timeit first_n_digits1(3423234239472523409283475908723908723409872390871243908172340987123409871234012089172340987734507612340981344509873123401234670350981234098123140987314509812734091823509871345109871234098172340987125988123452452, 39)
1.86 μs ± 21.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

# The "string" verions
def first_n_digits2(num, n):
    return int(str(num)[:n])

%timeit first_n_digits2(34523452452, 2)
744 ns ± 28.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%timeit first_n_digits2(34523452452, 8)
768 ns ± 42.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

# 22 digits
%timeit first_n_digits2(3423234239472523452452, 2)
767 ns ± 33.6 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%timeit first_n_digits2(3423234239472523452452, 15)
830 ns ± 55.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

# 196 digits
%timeit first_n_digits2(3423234239472523409283475908723908723409872390871243908098712340987123401208917234098773450761234098134450987312340123467035098123409812314098734091823509871345109871234098172340987125988123452452, 39)
1.87 μs ± 140 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

回答by ThisSuitIsBlackNot

You can use a regular expression to test for a match and capture the first two digits:

您可以使用正则表达式来测试匹配并捕获前两位数字:

import re

for i in range(1000):
    match = re.match(r'(1[56])', str(i))

    if match:
        print(i, 'begins with', match.group(1))

The regular expression (1[56])matches a 1 followed by either a 5 or a 6 and stores the result in the first capturing group.

正则表达式(1[56])匹配 1 后跟 5 或 6 并将结果存储在第一个捕获组中。

Output:

输出:

15 begins with 15
16 begins with 16
150 begins with 15
151 begins with 15
152 begins with 15
153 begins with 15
154 begins with 15
155 begins with 15
156 begins with 15
157 begins with 15
158 begins with 15
159 begins with 15
160 begins with 16
161 begins with 16
162 begins with 16
163 begins with 16
164 begins with 16
165 begins with 16
166 begins with 16
167 begins with 16
168 begins with 16
169 begins with 16