Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何在两个数组中获得所有不同的部分?_Arrays_String_List_Compare - Fatal编程技术网

Arrays 如何在两个数组中获得所有不同的部分?

Arrays 如何在两个数组中获得所有不同的部分?,arrays,string,list,compare,Arrays,String,List,Compare,假设我有一句话: When Grazia Deledda submitted a short story to a fashion magazine at the age of 13 这句话分为两部分: # list 1 [When] [Grazia Deledda] [submitted a short story] [to] [a] [fashion magazine] [at] [the age of] [13] # list 2 [When] [Grazia Deledda] [sub

假设我有一句话:

When Grazia Deledda submitted a short story to a fashion magazine at the age of 13
这句话分为两部分:

# list 1
[When] [Grazia Deledda] [submitted a short story] [to] [a] [fashion magazine] [at] [the age of] [13]
# list 2
[When] [Grazia Deledda] [submitted] [a short story] [to] [a fashion] [magazine at] [the age of] [13]
现在我想得到这两个数组中的不同部分,这个例子的结果应该是:

[
    ([[submitted a short story]],[[submitted] [a short story]]),
    ([[a] [fashion magazine] [at]], [[a fashion] [magazine at]])
]
因此,它应该满足以下要求:

  • 每一对都应该有相同的内容,例如:
    [[submitted a short story]]
    可以加入
    “submitted a short story”
    ,而
    [[submitted][a short story]]
    也可以加入
    “submitted a short story”

  • 每对都应该有相同的开始位置和结束位置,例如:
    [[提交了一篇短篇小说]]
    从3开始,以6结束。[[提交][一篇短篇小说]都是一样的

  • 最重要的是,每个人都应该是最短的,例如
    [[提交一篇短篇小说][to]]
    [[提交一篇短篇小说][to]]
    也满足前两个要求,但不是最短的


有什么方法可以避免O(n^2)复杂性吗?

一开始我可能会走错方向,这个问题很简单,我有个好主意:

#!/usr/bin/env python
# encoding: utf-8

# list 1
llist = [["When"], ["Grazia", "Deledda"], ["submitted", "a", "short", "story"], ["to"], ["a"], ["fashion", "magazine"], ["at"], ["the", "age", "of",], ["13"],]
# list 2
rlist = [["When"], ["Grazia", "Deledda"], ["submitted"], ["a", "short", "story"], ["to"], ["a", "fashion"], ["magazine", "at"], ["the", "age", "of",], ["13"],]

loffset = -1
roffset = 0
rindex = 0
lstart = -1
rstart = -1
for lindex, litem in enumerate(llist):
    if loffset == roffset and litem != rlist[rindex]:
        lstart = lindex
        rstart = rindex
    loffset += len(litem)
    while roffset < loffset:
        roffset += len(rlist[rindex])
        rindex += 1
    if loffset == roffset and lstart >= 0:
        print(llist[lstart:lindex+1], rlist[rstart:rindex])
        lstart = -1
!/usr/bin/env python
#编码:utf-8
#清单1
llist=[“当”]、[“格拉齐亚”、“德莱达”]、[“提交”、“a”、“短篇”、“故事”]、[“到”]、[“a”]、[“时尚”、“杂志”]、[“在”]、[“年龄”、“时代”、[“13”]、]
#清单2
rlist=[“当”]、[“格拉齐亚”、“德莱达”]、[“提交”]、[“a”、“短”、“故事”]、[“到”]、[“a”、“时尚”]、[“杂志”、[“在”]、[“年龄”、[“of”、]、[“13”]、]
loffset=-1
roffset=0
rindex=0
lstart=-1
rstart=-1
对于lindex,枚举中的litem(llist):
如果loffset==roffset和litem!=rlist[rindex]:
lstart=lindex
rstart=rindex
loffset+=len(litem)
roffset=0:
打印(llist[lstart:lindex+1],rlist[rstart:rindex])
lstart=-1