英文:
Find elegant solution for finding same items that follow each other in two lists
问题
I have multiple lists in list and I want to find out if any of them is at least a partial match.
Example:
list_of_lists = [["a", "b", "c"], ["z", "a", "b"], ["y", "b", "c"], ["z", "a"]]
The desired output should be:
["a", "b"] # because first and second list
["b", "c"] # because first and third list
["z", "a"] # because second and last list
❗The order and duplicated matters. So I cannot use set
. it chance to have desired output like that: ["a", "b", "a"]
I can probably loop over every item in 3 or more for cycles but for me is totally overkill and when I have 100 lists in list it will be really slow.
if exist any pandas or numpy function or something in Python that to do more efficiently I would appreciate it.
英文:
I have multiple lists in list and I want to find out if any of them is at least a partial match.
Example:
list_of_lists = [["a", "b", "c"], ["z", "a", "b"], ["y", "b", "c"], ["z", "a"]]
The desired output should be:
["a", "b"] # because first and second list
["b", "c"] # because first and third list
["z", "a"] # because second and last list
❗The order and duplicated matters. So I cannot use set
. it chance to have desired output like that: ["a", "b", "a"]
I can probably loop over every item in 3 or more for cycles but for me is totally overkill and when I have 100 lists in list it will be really slow.
if exist any pandas or numpy function or something in Python that to do more efficiently I would appreciate it.
答案1
得分: 2
我怀疑这可能不是可能的最高效解决方案,但找到了一个似乎可以工作的方法。检查它如何在你的列表中运行,如果需要调整,请留下评论。
它将列表合并为一个以逗号分隔的字符串,然后使用正则表达式搜索重复序列。
英文:
I suspect this isn't the highest-performance solution possible, but found a method that appears to work. Check how it works with your lists and drop a comment if it needs tweaking.
It combines the lists into a comma-delimited string then uses Regex to search for repeating sequences.
import re
list_of_lists = [["a", "b", "c"], ["z", "a", "b"], ["y", "b", "c"], ["z", "a"]]
search=re.compile(r'([a-z]{2,}).+?,?.+?') #
text=','.join([''.join(s) for s in list_of_lists])
i=0
matches=[]
results=[]
while len(text)>0:
if result := search.search(text):
if (span := tuple(x+i for x in result.span())) not in matches:
matches.append(span)
results.append(list(result.group(1)))
text=text[1:]
i+=1
results
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论