有没有更多的Pythonic方法来编写下面的代码,以便它迭代某些条件但是还保留了迭代的索引?
def TrieMatching(text, trie):
match_locations = []
location = 0
while text:
if PrefixTrieMatching(text, trie):
match_locations.append(location)
text = text[1:]
location += 1
Run Code Online (Sandbox Code Playgroud)
我总是喜欢列表理解.
def TrieMatching(text, trie):
match_locations = [
location
for location in range(len(text))
if PrefixTrieMatch(text[location:],trie)
]
Run Code Online (Sandbox Code Playgroud)