如何使用pythonre模块对整数进行数字过滤

如何使用pythonre模块对整数进行数字过滤,python,regex,filter,int,digital,Python,Regex,Filter,Int,Digital,我想使用pythonre模块按数字过滤整数 1 700 76093 71365 35837 75671 ^^ ||--------------------- this position should not be 6,7,8,9,0 |---------------------- this position should not be 5,6,7 代码: 我有两个问题: 1.是否可以从以下代码生成reexp字符串: thousand_pos

我想使用pythonre模块按数字过滤整数

    1
  700
76093
71365
35837
75671
 ^^                 
 ||--------------------- this position should not be 6,7,8,9,0 
 |---------------------- this position should not be 5,6,7
代码:

我有两个问题:

1.是否可以从以下代码生成reexp字符串:

thousand_position = set([1,2,3,4,5,1,1,1,1,1,1,1,1,1,1])
hundred_position  = set([1,2,3,4,8,9,0,1,2,3,2,3,1,2])
2.如何使reexp更简单,避免0前缀以下的错误

00700
00500          <--- this will also drops into the reexp, it is a 
                     bug because it has no kilo number
10700

reexp = r"\d[0-4,8-9][1-5]\d\d"
00700

00500您确定要使用
re
模块吗?你可以通过一些简单的数学运算了解你想要做什么

def valid_number(n):
  return 0 < n%1000/100 < 6 and not 5 >= n%10000/1000 >= 7

int_list = [1,700,76093,71365,35837,75671,]
result   = [x for x in int_list if valid_number(x)]

是否确实要使用
re
模块?你可以通过一些简单的数学运算了解你想要做什么

def valid_number(n):
  return 0 < n%1000/100 < 6 and not 5 >= n%10000/1000 >= 7

int_list = [1,700,76093,71365,35837,75671,]
result   = [x for x in int_list if valid_number(x)]

好的,首先,我将发布一些代码,这些代码实际上实现了您最初描述的功能:

>>> int_list=[1, 700, 76093, 71365, 35837, 75671]
>>> str_list = [str(i).zfill(5) for i in int_list]
>>> filtered =  [s for s in str_list if re.match('\d[0-4,8-9][1-5]\d\d', s)]
>>> filtered
['71365']
编辑:好的,我想我现在明白你的问题了。您可以使用
rjust
,而不是使用
zfill
,它将插入空格而不是零

>>> int_list=[1,700,76093,71365,35837,75671,500]
>>> str_list = [str(i).rjust(5) for i in int_list]
>>> re_str = '\d' + str(list(set([0, 1, 3, 4, 8, 9]))) + str(list(set([1, 2, 3, 4, 5]))) + '\d\d'
>>> filtered =  [s for s in str_list if re.match(re_str, s)]
>>> filtered
['71365']

我认为按照yan的建议在数学上这样做最终会更快,但也许你有使用正则表达式的理由

好的,首先,我将发布一些代码,这些代码实际上实现了您最初描述的功能:

>>> int_list=[1, 700, 76093, 71365, 35837, 75671]
>>> str_list = [str(i).zfill(5) for i in int_list]
>>> filtered =  [s for s in str_list if re.match('\d[0-4,8-9][1-5]\d\d', s)]
>>> filtered
['71365']
编辑:好的,我想我现在明白你的问题了。您可以使用
rjust
,而不是使用
zfill
,它将插入空格而不是零

>>> int_list=[1,700,76093,71365,35837,75671,500]
>>> str_list = [str(i).rjust(5) for i in int_list]
>>> re_str = '\d' + str(list(set([0, 1, 3, 4, 8, 9]))) + str(list(set([1, 2, 3, 4, 5]))) + '\d\d'
>>> filtered =  [s for s in str_list if re.match(re_str, s)]
>>> filtered
['71365']

我认为按照yan的建议在数学上这样做最终会更快,但也许你有使用正则表达式的理由

感谢您提供的快速纯数学解决方案,但我想使用re使非数学类的人的问题更简单,通过使用re和数字,我可以在以后添加一个带有0-9勾选框的UI,也许。。。还有,我可以知道n%在这里意味着什么吗?谢谢你提供了快速的纯数学解决方案,但我想使用re使问题对那些非数学的人来说更简单,通过使用re和数字,我可以稍后添加一个带有0-9勾选框的UI,也许。。。还有,我可以知道n%在这里是什么意思吗?谢谢你的回答,第二个问题在这里起作用,因为00700不是偶然地放入regexp'\d[0-4,8-9][1-5]\d\d',但是00500怎么样?@user478514:我已经修改了第二个版本,以实现我认为你想要的。谢谢你的回答,第二个问题在这里起作用,因为00700不是放入regexp'\d'[0-4,8-9][1-5]\d\d'碰巧,但00500怎么样?@user478514:我已经修改了第二个版本,以满足您的要求。仅供参考,请查看我编辑的答案。如果有任何问题,请告诉我。仅供参考,请查看我编辑的答案。如果有任何问题,请告诉我。