按照规则自动生成字符串

haiboyang 2014-09-05 03:40:56
现有字符串形如这种样式http://www.baidu.com/[10:100:2]/[5:10:1]/[A:E:2],想按照[ ]里面的生成字符串列表,
http://www.baidu.com/10/5/A
.................
http://www.baidu.com/100/10/E
其中[ ]中以:分隔的,第一个为起始值,第二位结束值,第三个为步长,希望大家能够提供思路,谢谢。
...全文
292 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
根据 #4楼意见 修改最终可行代码如下

# -*- 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
  • 打赏
  • 举报
回复
#求最终参数组合表 resultParamList = [] proIter= product(*replaceListList) #这里改成参数列表的传递方式,应该就可以支持不确定个数了
haiboyang 2014-09-09
  • 打赏
  • 举报
回复
我研究下代码,看怎么可配置,不是三个列表项的时候,怎么修改代码。
panghuhu250 2014-09-05
  • 打赏
  • 举报
回复
itertools模块有product函数,可以生成多个列表的笛卡尔积.

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 >>> 该有的都有了 剩下的可以自己遍历参数列表逐个把字符串拼起来

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧