ik_tech
ik_tech

Reputation: 3

Python - matching on nested list

I've got the following code;

#!/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)

The above code returns an empty list []

Essentially, what i'm trying to do is create a 3rd list which will contain only the unique values, which should look like this;

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

If anyone can guide me, that would be great.

Thanks guys

Upvotes: 0

Views: 89

Answers (5)

Aaditya Ura
Aaditya Ura

Reputation: 12689

Are you looking for something like this ?

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)

output:

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

If you don't want to pass one by one then :

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

print(no_dub)

output:

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

Update as per request :

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)

output:

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

Upvotes: 1

FatihAkici
FatihAkici

Reputation: 5109

I would also suggest flattening and then using set, but saw almost identical answers when I finished coding. Nevertheless, this is how I would do it:

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))

Result:

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

Upvotes: 0

RoadRunner
RoadRunner

Reputation: 26335

You can make a function that flattens a list, and then convert the result to a 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)))))

Which Outputs:

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

Upvotes: 0

Sohaib Farooqi
Sohaib Farooqi

Reputation: 5676

You first need to make flat list out of list1 and list2, make an extended list which contains both elements of list1 and list2 and cast the resulting list to set to remove duplicate elements.

Solution i am giving is little less verbose and involve less number of lines

#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]

Upvotes: 1

Ajax1234
Ajax1234

Reputation: 71481

I suggest using recursion to flatten your lists and then using a set:

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))]))

Output:

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

Upvotes: 2

Related Questions