Python - 在嵌套列表上匹配

问题描述 投票:0回答:5

我有以下代码;

#!/usr/bin/python

list1 = [[1,1,1,2], 2, [1,2,3,6], 3, 4, 5]
list2 = [1,1, [6,7], 7, 14 , 8, 9]
list3 = []


def check_match(l1,l2):
    for i in l1:
        if type(i) is list:
            check_match(i,l2)
        for j in l2:
            if type(j) is list:
                check_match(l1,j)
            else:
                if i == j:
                    list3.append(i)

print(list3)

上面的代码返回一个空列表[]

基本上,我要做的是创建一个第三个列表,它只包含唯一的值,它应该是这样的;

= 1,a,p,4,x,i,h,z,r,14]

如果有人能指导我,那就太好了。

多谢你们

python list loops for-loop
5个回答
1
投票

你在找这样的东西吗?

list1 = [[1,1,1,2], 2, [1,2,3,6], 3, 4, 5]
list2 = [1,1, [6,7], 7, 14 , 8, 9]


no_dub=[]
def checker(lst):
    if not lst:
        return 0
    else:
        for i in lst:
            if isinstance(i,list):
                for k in i:
                    if k not in no_dub:
                        no_dub.append(k)

            else:
                if i not in no_dub:
                    no_dub.append(i)

            return checker(lst[1:])

checker(list1)
checker(list2)
print(no_dub)

输出:

[1, 2, 3, 6, 4, 5, 7, 14, 8, 9]

如果您不想一个接一个地传递,那么:

check_all=[list1,list2]
for i in check_all:
    checker(i)

print(no_dub)

输出:

[1, 2, 3, 6, 4, 5, 7, 14, 8, 9]

根据要求更新:

list1 = [[1,1,1,2], 2, [1,2,3,6], 3, 4, 5]
list2 = [1,1, [6,7], 7, 14 , 8, 9]


no_dub=[]
def checker(lst):
    if not lst:
        return 0
    else:
        for i in lst:
            if isinstance(i,list):
                for k in i:
                    if "ID{}".format(k) not in no_dub:
                        no_dub.append("ID{}".format(k))

            else:
                if "ID{}".format(i) not in no_dub:
                    no_dub.append("ID{}".format(i))

            return checker(lst[1:])

check_all=[list1,list2]
for i in check_all:
    checker(i)

print(no_dub)

输出:

['ID1', 'ID2', 'ID3', 'ID6', 'ID4', 'ID5', 'ID7', 'ID14', 'ID8', 'ID9']

2
投票

我建议使用递归来展平列表,然后使用集合:

def flatten(s, target=int): 
   if type(s) == target:
       yield s
   else:
      for i in s:
         for b in flatten(i):
            yield b

list1 = [[1,1,1,2], 2, [1,2,3,6], 3, 4, 5]
list2 = [1,1, [6,7], 7, 14 , 8, 9]
final_result = list(set([*list(flatten(list1)), *list(flatten(list2))]))

输出:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 14]

1
投票

你首先需要从list1list2制作平面列表,制作一个包含list1list2元素的扩展列表,并将结果列表设置为删除重复元素。

我给出的解决方案略显冗长,涉及的线路数量较少

#Flatten list function
flatten=lambda l: sum(map(flatten,l),[]) if isinstance(l,list) else [l]
list1 = flatten(list1) #[1, 1, 1, 2, 2, 1, 2, 3, 6, 3, 4, 5]
list2 = flatten(list2) #[1, 1, 6, 7, 7, 14, 8, 9]
list1.extend(list2) #[1, 1, 1, 2, 2, 1, 2, 3, 6, 3, 4, 5, 1, 1, 6, 7, 7, 14, 8, 9]
list(set(list1))
#[1, 2, 3, 4, 5, 6, 7, 8, 9, 14]

0
投票

您可以创建一个使列表变平的函数,然后将结果转换为set()

list1 = [[1,1,1,2], 2, [1,2,3,6], 3, 4, 5]
list2 = [1,1, [6,7], 7, 14 , 8, 9]

def flatten(lst):
    flattened = []
    for item in lst:
        if isinstance(item, list):
            flattened.extend(flatten(item))
        else:
            flattened.append(item)

    return flattened

print(sorted(list(set(flatten(list1 + list2)))))

哪些输出:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 14] 

0
投票

我也建议展平然后使用set,但是当我完成编码时看到几乎相同的答案。不过,我就是这样做的:

list1 = [[1,1,1,2], 2, [1,2,3,6], 3, 4, 5]
list2 = [1,1, [6,7], 7, 14 , 8, 9]
l = list1+list2
flatList = []
for e in l:
    if type(e) == list:
        flatList.extend(e)
    else:
        flatList.append(e)
list(set(flatList))

结果:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 14]
© www.soinside.com 2019 - 2024. All rights reserved.