在更大的列表中查找空洞的子列表 [英] finding gappy sublists within a larger list

查看:85
本文介绍了在更大的列表中查找空洞的子列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个这样的列表:

Let's say I have a list like this:

 [['she', 'is', 'a', 'student'],
 ['she', 'is', 'a', 'lawer'],
 ['she', 'is', 'a', 'great', 'student'],
 ['i', 'am', 'a', 'teacher'],
 ['she', 'is', 'a', 'very', 'very', 'exceptionally', 'good', 'student']]

现在我有一个这样的列表:

Now I have a list like this:

['she', 'is', 'student']

我想用这个查询更大的列表,并以相同的顺序返回所有包含查询列表中单词的列表.可能存在差距,但顺序应相同.我怎样才能做到这一点?我尝试使用in运算符,但未获得所需的输出.

I want to query the larger list with this one, and return all the lists that contain the words within the query list in the same order. There might be gaps, but the order should be the same. How can I do that? I tried using the in operator but I don't get the desired output.

推荐答案

如果您只关心单词在数组中的排列顺序,则可以使用

If all that you care about is that the words appear in order somehwere in the array, you can use a collections.deque and popleft to iterate through the list, and if the deque is emptied, you have found a valid match:

from collections import deque

def find_gappy(arr, m):
  dq = deque(m)
  for word in arr:
    if word == dq[0]:
      dq.popleft()
      if not dq:
        return True
  return False

通过将arr中的每个worddq的第一个元素进行比较,我们知道,当找到匹配项时,将以正确的顺序找到匹配项,然后我们找到popleft,因此现在正在与deque中的下一个元素进行比较.

By comparing each word in arr with the first element of dq, we know that when we find a match, it has been found in the correct order, and then we popleft, so we now are comparing with the next element in the deque.

要过滤您的初始列表,您可以使用简单的列表理解功能,根据find_gappy的结果进行过滤:

To filter your initial list, you can use a simple list comprehension that filters based on the result of find_gappy:

matches = ['she', 'is', 'student']
x = [i for i in x if find_gappy(i, matches)]

# [['she', 'is', 'a', 'student'], ['she', 'is', 'a', 'great', 'student'], ['she', 'is', 'a', 'very', 'very', 'exceptionally', 'good', 'student']]

这篇关于在更大的列表中查找空洞的子列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆