python reモジュールを使用して、int数値をデジタル数値でフィルタリングしたいと考えています。
1
700
76093
71365
35837
75671
^^
||--------------------- this position should not be 6,7,8,9,0
|---------------------- this position should not be 5,6,7
コード:
int_list=[1,700,76093,71365,35837,75671]
str_list = [str(x).zfill(5) for x in int_list]
reexp = r"\d[0-4,8-9][1-5]\d\d"
import re
p = re.compile(reexp)
result = [int("".join(str(y) for y in x)) for x in str_list if p.match(x)]
2 つの質問があります。
1.以下のコードから再表現文字列を生成することは可能ですか:
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.再式をより単純にする方法は、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"
御時間ありがとうございます
B.Rgs
PS:以下の数学ソリューションの提案に感謝します。簡単で高速な場合があることはわかっていますが、reベースのバージョンで他の考えのバランスを取りたいです。