查找列表元素的最接近值

Find the nearest value of a list element

我有两个列表:

>>> list1 = ['gain','archive','win','success']
>>> list2 = ['i','win','game','i','am','success','cool']

而且我通过比较列表发现了两个列表的相同值。

>>> result= set(list1) & set(list2)

输出是

set(['win', 'success'])

现在我想找到 resultnext 元素值。在这里它将是:'game''cool'.

我该怎么做(使用 python 2.7)?

假设你有交集的话

result = { 'win', 'success' }

您可以像这样在 list2 中找到下一个单词:

next_words = [list2[list2.index(word)+1] for word in result]

index 获取列表中给定元素的索引。您可以对其加 1 以获得下一个元素。

如果你的元素在列表的,它会抛出异常,因为没有"next"元素可以获取。

您可以使用 index 函数并添加 1。但是请注意,如果您的公共元素是列表的最后一个,则会产生错误

list1 = ['gain','archive','win','success']
list2 = ['i','win','game','i','am','success','cool']
result= set(list1) & set(list2)

list3 = [list2[list2.index(e)+1] for e in result]

编辑 对于最后一个元素是公共元素的情况:

result= set(list1) & set(list2)
list4 = []
for e in result:
    try:
        list4.append(list2[list2.index(e)+1])
    except:
        pass

输出:['game', 'cool']

您可以使用 list2.index,但这只是为了找回索引而进行的全面搜索,并人为地将复杂性从 O(n) 增加到 O(n*n)

只需跟踪每个单词的索引即可。有几种方法可以做到这一点。

  • 创建您自己的搜索常用词的函数,并将它们 return 作为 list2 中那些词的索引。这可能是最少的 pythonic 但最快的。

  • list2的词到它们的索引创建一个字典,然后在计算集合交集后,在字典上查找索引并增加一。你需要构建一个 list2 大小的完整字典,这可能很昂贵(但仍然比 O(n*n) 好)。

  • list2 的单词到它们的下一个单词或 None 的单词创建字典(如果没有),然后在字典中查找索引。您需要构建一个 list2 大小的完整字典,这可能很昂贵。

  • 如果你知道如何使用 itertools,你可以在 list2 上做一个迭代器,它产生索引和单词,如果单词在list1,然后只选择索引。

这对 list2 中的下一个元素有效:

next_result = [list2[list2.index(el)+1] for el in result if list2.index(el)+1<len(list2)]

您可以对 list2 进行成对迭代并手动执行 "intersection":

list1 = ['gain','archive','win','success']
list2 = ['i','win','game','i','am','success','cool']

set1 = set(list1)

result = []
for item, nextitem in zip(list2, list2[1:]):  # pairwise iteration
    if item in set1:
        result.append(nextitem)   # append the next item if the current item is in the intersection

print(result)  # ['game', 'cool']