我有一个连字符字符串列表:
(样本)
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--------------------
谢谢
稍微浏览一下itertools提供的工具,我发现这个产品在这里可能最有用。它让我们了解在两个单词之间有空格或破折号的所有可能性
另外,根据您的要求,我将跳过任何两个单词之间没有破折号的迭代
输出:
只需制作自己的生成器来生成组合,可能会更容易一些。这可以通过递归生成器以非常可读的方式完成,只要字符串不足以运行到堆栈限制:
结果:
有了它,您可以在理解中使用它,或者将它传递给其他
itertools
函数来执行您需要的任何操作:相关问题 更多 >
编程相关推荐