python - python字符串按分隔符拆分所有可能的排列
问题描述
这可能与Python 3.3: Split string and create all combination等类似问题密切相关,但我无法从中推断出 Pythonic 解决方案。
问题是:
假设有一个 str 'hi|guys|whats|app'
,我需要用分隔符拆分该 str 的所有排列。例子:
#splitting only once
['hi','guys|whats|app']
['hi|guys','whats|app']
['hi|guys|whats','app']
#splitting only twice
['hi','guys','whats|app']
['hi','guys|whats','app']
#splitting only three times
...
etc
我可以编写一个回溯算法,但是 python(例如 itertools)是否提供了一个简化该算法的库?
提前致谢!!
解决方案
一种方法,一旦你拆分了字符串,就是用来itertools.combinations
定义列表中的拆分点,其他位置应该再次融合。
def lst_merge(lst, positions, sep='|'):
'''merges a list on points other than positions'''
'''A, B, C, D and 0, 1 -> A, B, C|D'''
a = -1
out = []
for b in list(positions)+[len(lst)-1]:
out.append('|'.join(lst[a+1:b+1]))
a = b
return out
def split_comb(s, split=1, sep='|'):
from itertools import combinations
l = s.split(sep)
return [lst_merge(l, pos, sep=sep)
for pos in combinations(range(len(l)-1), split)]
例子
>>> split_comb('hi|guys|whats|app', 0)
[['hi|guys|whats|app']]
>>> split_comb('hi|guys|whats|app', 1)
[['hi', 'guys|whats|app'],
['hi|guys', 'whats|app'],
['hi|guys|whats', 'app']]
>>> split_comb('hi|guys|whats|app', 2)
[['hi', 'guys', 'whats|app'],
['hi', 'guys|whats', 'app'],
['hi|guys', 'whats', 'app']]
>>> split_comb('hi|guys|whats|app', 3)
[['hi', 'guys', 'whats', 'app']]
>>> split_comb('hi|guys|whats|app', 4)
[] ## impossible
理由
ABCD -> A B C D
0 1 2
combinations of split points: 0/1 or 0/2 or 1/2
0/1 -> merge on 2 -> A B CD
0/2 -> merge on 1 -> A BC D
1/2 -> merge on 0 -> AB C D
泛型函数
这是一个通用版本,像上面一样工作,但也-1
作为参数 for split
,在这种情况下,它将输出所有组合
def lst_merge(lst, positions, sep='|'):
a = -1
out = []
for b in list(positions)+[len(lst)-1]:
out.append('|'.join(lst[a+1:b+1]))
a = b
return out
def split_comb(s, split=1, sep='|'):
from itertools import combinations, chain
l = s.split(sep)
if split == -1:
pos = chain.from_iterable(combinations(range(len(l)-1), r)
for r in range(len(l)+1))
else:
pos = combinations(range(len(l)-1), split)
return [lst_merge(l, pos, sep=sep)
for pos in pos]
例子:
>>> split_comb('hi|guys|whats|app', -1)
[['hi|guys|whats|app'],
['hi', 'guys|whats|app'],
['hi|guys', 'whats|app'],
['hi|guys|whats', 'app'],
['hi', 'guys', 'whats|app'],
['hi', 'guys|whats', 'app'],
['hi|guys', 'whats', 'app'],
['hi', 'guys', 'whats', 'app']]
推荐阅读
- r - R - 地图缩放功能,使绘图成为圆形而不是正方形
- python - 在从 instagram 抓取一些图像的 url 后,我想获得全分辨率图像
- python-3.x - 迭代错误期间字典大小更改
- angular - 向 formBuilder 添加不需要的字段
- python - 如何拆分时间数据(格式为 yyyy-mm-dd hh:mm:ss)来测试和训练集?
- javascript - 我想将三个.js 渲染的粒子包含在一个 div 中,但它不起作用
- javascript - .load 带有数据参数的 jQuery 方法,用于 Partial Razor 页面
- cmake - 链接到链接到另一个静态库的已构建静态库
- python - python中轨迹估计的最大似然估计
- c# - 我需要在 SSIS 中获取数据湖文件的文件最后修改日期