如何相互比较嵌套列表的元素?

How to compare elements of nested lists between each other?

我尝试比较嵌套列表的元素。假设我有以下列表:

list1 = [['v1', '1', '2'],['v1', '2', '2'], ['v2', '1'], ['v3'], ['v4', '1'], ['v4', '2']]

我想联系:

result = [['v1', '2'],['v1', '2'],['v2', '1'], ['v3'], ['v4'], ['v4']]

我做了一个小代码,但看起来效果不是很好。

for i in range(1, len(list1) - 1):
    previousone = list1[i-1]
    currentone = list1[i]
    nextone = list1[i+1]
    lenprevious = len(previousone)
    lencurrent = len(currentone)
    lennext = len(nextone)
    minlen = min(lenprevious,lencurrent,lennext) -1
    common = ''
    for j in range(minlen):
        if j == 0:
            if previousone[j] == currentone[j]:
                common += str(previousone[j])
            if previousone[j] != currentone[j]:
                if currentone[j] == nextone[j]:
                    common += str(currentone[j])
                else:
                    common += currentone
                    break
        else:
            if common != '':
                if previousone[j] == currentone[j]:
                    common.join('_',str(nextone[j]))
                else:
                    if currentone[j] == nextone[j]:
                        common.join('_',str(nextone[j]))
                    else:
                        break
            else:
                break
    print common
    result.append(common)

这个想法是比较子列表的第一个元素与前一个子列表的第一个元素。如果没有匹配,则我们与下一个子列表进行比较。如果不匹配,我们将在 common 中获取当前子列表的第一个元素。 然后,如果它匹配,我们对子列表的下一个元素做同样的事情,直到最后一个。最后,我希望在 common 中有一个公共元素列表(如果有的话),如果没有,我想要当前的子列表。 有谁知道如何让它工作?提前致谢!

编辑:: 逻辑是:

Iteration 1 -> Previous : ['v1', '1', '2'] and Current : ['v1', '2', '2'] and Next : ['v2', '1']

我们比较每个列表中的每个元素。 首先,我们比较 Previous 和 Current。 这些列表的第一个元素是 'v1',因此我们在结果中附加 'v1' 并转到下一个元素,此处为“1”和“2”。 它们不相同,所以我们一直传递到下一个元素,即 '2' 和 '2':相同。 我们追加结果得到结果:

[['v1', '2'], ['v1', '2'], [], [], [], []]

Iteration 2 -> Previous : ['v1', '2', '2'] and Current : ['v2', '1'] and Next : ['v3']

首先我们比较以前和现在。 'v1' 不同于 'v2'。 所以我们比较 Current 和 Next。 'v2' 不同于 'v3'。 所以我们在结果中附加当前值,我们得到:

[['v1', '2'], ['v1', '2'], ['v2', '1'], [], [], []]

Iteration 3 -> Previous : ['v2', '1'] and Current : ['v3'] and Next : ['v4', '1']

同上,'v2'不同于'v3','v3'不同于'v4',所以我们追加当前并得到:

[['v1', '2'], ['v1', '2'], ['v2', '1'], ['v3'], [], []]

Iteration 4 -> Previous : ['v3'] and Current : ['v4', '1'] and Next: ['v4', '2']

'v3' 不同于 'v4' 所以我们比较 Current 和 Next: 'v4' 很常见所以我们追加 'v4':

[['v1', '2'], ['v1', '2'], ['v2', '1'], ['v3'], ['v4'], []]

Iteration 5 -> Previous : ['v4', '1'] and Current : ['v4', '2'] and Next : ??

'v4' 很常见,所以我们追加 'v4' 并得到最终结果:

Result: [['v1', '2'], ['v1', '2'], ['v2', '1'], ['v3'], ['v4'], ['v4']]

但是我不知道怎么去..

下面是根据你想要的结果实现的,

核心逻辑

def intersection(lst1, lst2): 
    return list(set(lst1) & set(lst2)) 


list1 = [['v1', '1', '2'],['v1', '2', '2'], ['v2', '1'], ['v3'], ['v4', '1'], ['v4', '2']]
result = []
list_len = len(list1)

if list_len == 0:
    pass
elif list_len == 1:
    result.append(list1)
else:
    for i in range(list_len):

        if i == 0:
            current = list1[i]
            next = list1[i+1]
            print("Iteration {} -> Previous : No previous available Current : {} Next: {}".format(i, current, next))
            if current[0] == next[0]:
                result.append(intersection(current, next))
            else:
                result.append(current)    

        elif i == list_len - 1:
            previous = list1[i-1]
            current = list1[i]
            print("Iteration {} -> Previous : {} Current : {} Next: No next available".format(i, previous, current))
            if current[0] != previous[0]:
                result.append(current)
            else:
                result.append(intersection(current, previous))

        else:
            previous = list1[i-1]
            current = list1[i]
            next = list1[i+1]
            print("Iteration {} -> Previous : {} Current : {} Next: {}".format(i, previous, current, next))
            if current[0] == previous[0]:
                result.append(intersection(current, previous))
            else:
                if current[0] == next[0]:
                    result.append(intersection(current, next))
                else:
                    result.append(current)



print("Result : {}".format(result))

输出

Iteration 0 -> Previous : No previous available Current : ['v1', '1', '2'] Next: ['v1', '2', '2']
Iteration 1 -> Previous : ['v1', '1', '2'] Current : ['v1', '2', '2'] Next: ['v2', '1']
Iteration 2 -> Previous : ['v1', '2', '2'] Current : ['v2', '1'] Next: ['v3']
Iteration 3 -> Previous : ['v2', '1'] Current : ['v3'] Next: ['v4', '1']
Iteration 4 -> Previous : ['v3'] Current : ['v4', '1'] Next: ['v4', '2']
Iteration 5 -> Previous : ['v4', '1'] Current : ['v4', '2'] Next: No next available
Result : [['v1', '2'], ['v1', '2'], ['v2', '1'], ['v3'], ['v4'], ['v4']]