[英]finding gappy sublists within a larger list
假設我有一個這樣的列表:
[['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']]
現在我有一個像這樣的列表:
['she', 'is', 'student']
我想用這個查詢更大的列表,並以相同順序返回所有包含查詢列表中單詞的列表。 可能存在差距,但順序應相同。 我怎樣才能做到這一點? 我嘗試使用in
運算符,但未獲得所需的輸出。
如果您只關心單詞在數組中的出現順序,則可以使用collections.deque
和popleft
遍歷列表,如果將deque
為空,則找到有效的匹配項:
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
每個word
與dq
的第一個元素進行比較,我們知道,當找到匹配項時,就以正確的順序找到了匹配項,然后我們popleft
,因此現在我們與deque
的下一個元素進行比較。
要過濾您的初始列表,您可以使用一個簡單的列表理解功能,該功能基於find_gappy
的結果進行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']]
您可以比較兩個列表,並具有類似這樣的功能。 它的工作方式是循環遍歷您的較短列表,並且每次在長列表中找到下一個單詞時,都將切斷較長列表的第一部分。 如果找不到該單詞,則返回false。
def is_sub_sequence(long_list, short_list):
for word in short_list:
if word in long_list:
i = long_list.index(word)
long_list = long_list[i+1:]
else:
return False
return True
現在,您可以使用一個函數來告訴您列表是否為所需類型,您可以使用如下列表理解功能從“列表列表”中過濾出所需的所有列表:
a = [['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']]
b = ['she', 'is', 'student']
filtered = [x for x in a if is_sub_sequence(x,b)]
filtered
的列表將僅包括所需類型的列表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.