【发布时间】:2020-04-24 20:21:48
【问题描述】:
我有一个连字符字符串列表:
(样本)
myList = ['mother-in-law', 'co-operation', 'sixty-nine-eighty-ninths']
对于此列表的每个元素,我希望能够创建连字符位于每个元素的两个或多个标记之间的所有变体:
mother-in law
mother in-law
sixty-nine eighty ninths
sixty-nine-eighty ninths
sixty nine-eighty-ninths
sixty-nine eighty-ninths
sixty nine-eighty ninths
sixty nine eighty-ninths
...
我尝试了这个问题的解决方案 (Create variations of a string) 但我不知道如何适应它:
from itertools import combinations
myList = ['mother-in-law', 'co-operation', 'sixty-nine-eighty-ninths']
for e in myList :
for i in range(len(e.split("-"))):
for indices in combinations(range(len(e.split("-"))), i):
print(''.join([e.split("-")[x] if x in indices else '-' for x in range(len(e))]))
这就是我得到的:
-------------
mother------------
-in-----------
--law----------
motherin-----------
mother-law----------
-inlaw----------
------------
co-----------
-operation----------
------------------------
sixty-----------------------
-nine----------------------
--eighty---------------------
---ninths--------------------
sixtynine----------------------
sixty-eighty---------------------
sixty--ninths--------------------
-nineeighty---------------------
-nine-ninths--------------------
--eightyninths--------------------
sixtynineeighty---------------------
sixtynine-ninths--------------------
sixty-eightyninths--------------------
-nineeightyninths--------------------
谢谢
【问题讨论】:
标签: python string variations