Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python整数前缀范围算法_Python_Regex_Type Conversion - Fatal编程技术网

Python整数前缀范围算法

Python整数前缀范围算法,python,regex,type-conversion,Python,Regex,Type Conversion,这是我的问题。我有各种长度不同的整数输入,但我只希望打印某些数字前缀。前缀范围为20000-20150所有五位数字。到目前为止,我已经: print 20000 <= prefix <= 20150 您可以将对象设置为字符串(使用str),将其前5个字符切掉(作为前缀),然后将其转换回整数(使用int): def有效前缀(数字): 返回20000非常简单: >>> num = 20000201501 >>> print int(str(num)[

这是我的问题。我有各种长度不同的整数输入,但我只希望打印某些数字前缀。前缀范围为20000-20150所有五位数字。到目前为止,我已经:

print 20000 <= prefix <= 20150

您可以将对象设置为字符串(使用
str
),将其前5个字符切掉(作为前缀),然后将其转换回整数(使用
int
):

def有效前缀(数字):
返回20000非常简单:

>>> num = 20000201501
>>> print int(str(num)[:5])
20000
所以,你需要这样的东西:

def prefix(num):
    return int(str(num)[:5])

print 20000 < prefix(20000201501) < 20150
def前缀(num):
返回int(str(num)[:5])
打印20000<前缀(20000201501)<20150
def valid_prefix(number):
    return 20000 <= int(number) <= 20150

#obj is string:
obj = "20000201501"
print valid_prefix(obj[:5])

#obj is number:
obj = 20000201501
print valid_prefix(str(obj)[:5])
>>> num = 20000201501
>>> print int(str(num)[:5])
20000
def prefix(num):
    return int(str(num)[:5])

print 20000 < prefix(20000201501) < 20150