我正在尝试探索Python内置函数的功能.我正在尝试处理一些字符串,例如:
'the fast dog'
Run Code Online (Sandbox Code Playgroud)
并将字符串分解为所有可能的有序短语,如列表.上面的示例将输出如下:
[['the', 'fast dog'], ['the fast', 'dog'], ['the', 'fast', 'dog']]
Run Code Online (Sandbox Code Playgroud)
关键是在生成可能的短语时需要保留字符串中单词的原始顺序.
我已经能够得到一个可以做到这一点的功能,但它相当麻烦和丑陋.但是,我想知道Python中的某些内置功能是否有用.我当时认为可以将字符串分割为各种空格,然后以递归方式应用于每个分割.可能有人有一些建议吗?
fal*_*tru 10
import itertools
def break_down(text):
words = text.split()
ns = range(1, len(words)) # n = 1..(n-1)
for n in ns: # split into 2, 3, 4, ..., n parts.
for idxs in itertools.combinations(ns, n):
yield [' '.join(words[i:j]) for i, j in zip((0,) + idxs, idxs + (None,))]
Run Code Online (Sandbox Code Playgroud)
例:
>>> for x in break_down('the fast dog'):
... print(x)
...
['the', 'fast dog']
['the fast', 'dog']
['the', 'fast', 'dog']
>>> for x in break_down('the really fast dog'):
... print(x)
...
['the', 'really fast dog']
['the really', 'fast dog']
['the really fast', 'dog']
['the', 'really', 'fast dog']
['the', 'really fast', 'dog']
['the really', 'fast', 'dog']
['the', 'really', 'fast', 'dog']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1690 次 |
| 最近记录: |