基于模式在字符串中进行排列

时间:2017-07-06 17:09:52

标签: python itertools

输入列表:(仅当索引[1]具有/)

ListView

退货清单:

['A', 'dog,big/small;rat,big/small', '12', '21']

1 个答案:

答案 0 :(得分:0)

这个适用于该类型的一般输入字符串:

def permute(s):
    a, b = s.split(',')
    return [a + "," + c for c in b.split('/')]

def traverse(s):
    parts = s.split(';', 1)
    if len(parts) == 1:
        return permute(s)
    else:
        return sum([[k + ';' + l for l in traverse(parts[1])] for k in permute(parts[0])], [])

for s in traverse('dog,big/small;rat,big/small'):
    print ['A', s, '12', '21']