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

时间:2016-12-21 20:23:15

标签: python

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

for i in range(1000):

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

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

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

这样的命令

4 个答案:

答案 0 :(得分:34)

您可以将您的号码转换为字符串并使用列表切片,如下所示:

int(str(number)[:2])

<强>输出:

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

答案 1 :(得分:12)

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

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

功能:

import math

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

输出:

>>> 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

如果您的输入数字可能少于您想要的数字,则需要添加一些支票。

答案 2 :(得分:1)

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

import re

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

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

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

输出:

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

答案 3 :(得分:0)

将O(n)时间解决方案与其他答案中提供的“恒定时间” O(1)解决方案进行比较表明,如果O(n)算法足够快,则 n 必须比变慢的O(1)变大。

字符串版本约为。对于 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)