Click here to Skip to main content
15,359,191 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to get into each element in a nested list and compare it with the equivalent in all elements in another list.

I have two nested lists:

LB=
[[1, 0, 1, 0],
[1, 1, 1, 1],
[0, 0, 1, 0]]

LK=
[[1, 0, 1, 0],
[0, 0, 1, 1],
[1, 1, 1, 1],
[0, 0, 1, 0],
[1, 1, 0, 0],
[0, 0, 0, 0] ]

Analysis flow: I take the first sublist from LK (i.e., [1, 0, 1, 0]) and compare its individual elements (i.e., 1, 0, 1, 0) with subsequent elements from the sublists LB [1, 0, 1, 0], [1, 1, 1, 1], and [0, 0, 1, 0]. I then take the second sublist from LK (i.e., [0, 0, 1, 1]) and compare its individual elements (i.e., 0, 0, 1, 1) with the subsequent elements from the LB sublist [1, 0, 1, 0], [1, 1, 1, 1, 1], and [0, 0, 1, 0] I then take the third sublist from LK (i.e., [1, 1, 1, 1]) and compare its individual elements (i.e., 1, 1, 1, 1) with the subsequent elements from the LB sublist [1, 0, 1, 0], [1, 1, 1, 1, 1], and [0, 0, 1, 0]



Additional conditions:

    if a given element in a list in LK is equal to 0 and the digit at the same position in LB is also equal to 0 then proceed to compare the next digit in the given list. When you get to the end of the sublist in the LK list, i.e. you pass [1, 0, 1, 0] and the condition with zeroes is fulfilled, write me this element from the LB list
    if given element in the list in LK is equal 0, but digit in the same position in the list LB is not equal 0 then skip this case and check another sublist from LB
    if there is no element 0 in the list LK (list [1, 1, 1, 1]) then write a sublist from LB

We perform such an iteration for all the elements in the list LB comparing them with the sublists from LK.

In the analyzed example during the comparison:

    the first sublist from LK ([1, 0, 1, 0]) => should be extracted from the list LB: [1, 0, 1, 0] and [0, 0, 1, 0]
    the second sublist from LK ([0, 0, 1, 1]) => should be printed from the list LB: [0, 0, 1, 0]
    the third sublist from LK ([1, 1, 1, 1, 1]) => should be printed from the lists LB: [1, 0, 1, 0], [1, 1, 1, 1] and [0, 0, 1, 0]
    the fourth sublist from LK ([0, 0, 1, 0]) => should be printed from the list LB: [0, 0, 1, 0]
    the fifth sublist from LK ([1, 1, 0, 0]) => no element should be written out from the list LB
    sixth sublist from LK ([0, 0, 0, 0]) => no element should be written out from the list LB


What I have tried:

Here is my attempt at the code, but I know it doesn't work properly and doesn't contain all the conditions.

    for i in LK, LB:
       for j in i:
         while j < (len(LK)-1):
            if j == 0:
                j += 1
            elif j != 0:
                i += 1
            else:
                print(LB[i])
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900