37,720
社区成员
发帖
与我相关
我的任务
分享
# -*- coding: cp936 -*-
strS = '''http://www.baidu.com/[10:100:2]/[5:10:1]/[A:E:2]'''
from itertools import product
import re
#获取参数列表
splitList = re.findall(r".*?(\[.*?:.*?:.*?\])", strS)
#print splitList
paramList = []
for splitStr in splitList:
paramList.append( (splitStr[1:len(splitStr)-1:]).split(':'))
#print paramList
#获取替换用模板
templateList = re.split(r"\[.*?:.*?:.*?\]", strS)
#print templateList
templateStr = templateList[0]
for i in xrange(1,len(templateList)):
templateStr += ('%s'+ templateList[i])
#print templateStr
#求可以有效组合数
totalCnt = 1
replaceListList = []
for param in paramList:
startS = param[0]
stopS = param[1]
stepS = param[2]
if startS.isdigit():
start = int(startS)
stop = int(stopS)
step = int(stepS)
replaceList = range(start,stop+step,step)
elif startS.isalpha():
start = ord(startS)
stop = ord(stopS)
step = int(stepS)
replaceList = range(start,stop+step,step)
replaceList = [chr(x) for x in replaceList]
totalCnt *= (stop+step-start)/step
#print replaceList
replaceListList.append(replaceList)
#print totalCnt
#求最终参数组合表
resultParamList = []
proIter= product(replaceListList[0],replaceListList[1],replaceListList[2])
while True:
try:
resultParamList.append(proIter.next())
except:
break
#print resultParamList
#求最终字符串列表
resultStrList = []
for a,b,c in resultParamList:
resultStr = templateStr%(a,b,c)
resultStrList.append(resultStr)
print resultStr
>>>
http://www.baidu.com/10/5/A
http://www.baidu.com/10/5/C
http://www.baidu.com/10/5/E
http://www.baidu.com/10/6/A
http://www.baidu.com/10/6/C
http://www.baidu.com/10/6/E
http://www.baidu.com/10/7/A
http://www.baidu.com/10/7/C
http://www.baidu.com/10/7/E
....
http://www.baidu.com/100/8/C
http://www.baidu.com/100/8/E
http://www.baidu.com/100/9/A
http://www.baidu.com/100/9/C
http://www.baidu.com/100/9/E
http://www.baidu.com/100/10/A
http://www.baidu.com/100/10/C
http://www.baidu.com/100/10/E
In [63]: from itertools import product
In [64]: product?
Type: type
String Form:<type 'itertools.product'>
Docstring:
product(*iterables) --> product object
Cartesian product of input iterables. Equivalent to nested for-loops.
For example, product(A, B) returns the same as: ((x,y) for x in A for y in B).
The leftmost iterators are in the outermost for-loop, so the output tuples
cycle in a manner similar to an odometer (with the rightmost element changing
on every iteration).
To compute the product of an iterable with itself, specify the number
of repetitions with the optional repeat keyword argument. For example,
product(A, repeat=4) means the same as product(A, A, A, A).
product('ab', range(3)) --> ('a',0) ('a',1) ('a',2) ('b',0) ('b',1) ('b',2)
product((0,1), (0,1), (0,1)) --> (0,0,0) (0,0,1) (0,1,0) (0,1,1) (1,0,0) ...
# -*- coding: cp936 -*-
strS = '''http://www.baidu.com/[10:100:2]/[5:10:1]/[A:E:2]'''
import re
#获取参数列表
splitList = re.findall(r".*?(\[.*?:.*?:.*?\])", strS)
#print splitList
paramList = []
for splitStr in splitList:
paramList.append( (splitStr[1:len(splitStr)-1:]).split(':'))
print paramList
#获取替换用模板
templateList = re.split(r"\[.*?:.*?:.*?\]", strS)
#print templateList
templateStr = templateList[0]
for i in xrange(1,len(templateList)):
templateStr += ('%s'+ templateList[i])
print templateStr
#求可以有效组合数
totalCnt = 1
replaceListList = []
for param in paramList:
startS = param[0]
stopS = param[1]
stepS = param[2]
if startS.isdigit():
start = int(startS)
stop = int(stopS)
step = int(stepS)
replaceList = range(start,stop+step,step)
elif startS.isalpha():
start = ord(startS)
stop = ord(stopS)
step = int(stepS)
replaceList = range(start,stop+step,step)
replaceList = [chr(x) for x in replaceList]
totalCnt *= (stop+step-start)/step
print replaceList
replaceListList.append(replaceList)
print totalCnt
>>>
[['10', '100', '2'], ['5', '10', '1'], ['A', 'E', '2']]
http://www.baidu.com/%s/%s/%s
[10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]
[5, 6, 7, 8, 9, 10]
['A', 'C', 'E']
828
>>>
#接上文
#求可以有效组合数
totalCnt = 1
for param in paramList:
startS = param[0]
stopS = param[1]
stepS = param[2]
if startS.isdigit():
start = int(startS)
stop = int(stopS)
step = int(stepS)
elif startS.isalpha():
start = ord(startS)
stop = ord(stopS)
step = int(stepS)
totalCnt *= (stop+step-start)/step
print totalCnt
# -*- coding: cp936 -*-
import re
strS = '''http://www.baidu.com/[10:100:2]/[5:10:1]/[A:E:2]'''
#获取参数列表
splitList = re.findall(r".*?(\[.*?:.*?:.*?\])", strS)
print splitList
paramList = []
for splitStr in splitList:
paramList.append( (splitStr[1:len(splitStr)-1:]).split(':'))
print paramList
#获取替换用模板
templateList = re.split(r"\[.*?:.*?:.*?\]", strS)
print templateList
templateStr = templateList[0]
for i in xrange(1,len(templateList)):
templateStr += ('%s'+ templateList[i])
print templateStr
>>>
['[10:100:2]', '[5:10:1]', '[A:E:2]']
[['10', '100', '2'], ['5', '10', '1'], ['A', 'E', '2']]
['http://www.baidu.com/', '/', '/', '']
http://www.baidu.com/%s/%s/%s
>>>
该有的都有了 剩下的可以自己遍历参数列表逐个把字符串拼起来