将键(都出现在给定的字符串中)映射到字符串中的位置

2024-09-18 01:45:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试获取字符串中键的所有索引并将它们存储在dict中,以便 每个索引都有一个映射到它的键列表。你知道吗

示例:

string = "loloo and foofoo at the foo bar"
keys = "foo", "loo", "bar", "lo"

我想是这样的

{ 
  0: [lo]
  2: [loo, lo]
 10: [foo]
 13: [foo]
 24: [foo]
 28: [bar]
}

我目前的答案如下:

def get_index_for_string(string, keys):
    """
    Get all indexes of the keys in the string and store them in a dict, so that
    every index has a list of keys mapping to it.
    """
    key_in_string = dict((key, [m.start() for m in re.finditer(key, string)])
                            for key in keys if key in string)
    index_of_keys = {}
    for key, values in key_in_string.items():
        for value in values:
            if not value in index_of_keys:
                index_of_keys[value] = []
            index_of_keys[value].append(key)
    return index_of_keys

有什么好的建议吗?你知道吗


Tags: andofthekeyinloforstring
3条回答

首先,您需要re.escape键,以防它包含句点或类似的内容。除此之外,您还可以采取更直接的方法来构建结果dict:

from collections import defaultdict
def get_index_for_string(string, keys):
    res = defaultdict(list)
    for key in keys:
        for match in re.finditer(re.escape(key), string):
            res[match.start()].append(key)
    return res

注意:除了使用defaultdict之外,您还可以使用常规dict并执行res.setdefault(match.start(), []).append(key),但它看起来并不漂亮。你知道吗

你在寻找什么样的“更好”?如果需要更好的Big-O复杂性,可以使用Aho-Corasic Automaton。Python提供了一些快速实现:

Non-regex方法:

使用str.find()str.find()接受可选的第二个参数,该参数是要在其后面查找单词的索引。你知道吗

def indexes(word,strs):
    ind=0                #base index is 0
    res=[]
    while strs.find(word,ind)!=-1:   #loop until str.find() doesn't return -1
        ans=strs.find(word,ind)
        res.append(ans)
        ind=ans+1                 #change base index if the word is found
    return res     

strs = "loloo and foofoo at the foo bar"
keys = ["foo", "loo", "bar", "lo"]

print {x:indexes(x,strs) for x in keys}

输出:

{'lo': [0, 2], 'foo': [10, 13, 24], 'bar': [28], 'loo': [2]}

相关问题 更多 >