Matching 2 sublists

#1

Hi, I have a list made by sublists and I would like to match some items with another list.
Attached an example. Could somebody help me?

0 Likes

#2

Take Last Items of list 2 using List.LastItem node with lacing set to longest and then use Add.ItemsToEnd node with lacing to longest to add items at the end of each Sub List. Hope this what you want to achieve?

0 Likes

#3

hi robertoruggeri
try this out


have a good day

0 Likes

#4

Hello @dineshsubramani. Thanks for your attempt but unfortunately, that isn’t what he is asking for. He wants to add the item in List 2 to the matching list in List 1. Yours just puts the item into any list.

For example, the list at the bottom, [[1, -138.82], [5, -138.9]], the second list has 5 in the first index so he wants the -138.9 attached to the list in List 1 with 5 at index 0. Yours just adds it directly into the next list that happens to have 2 at index 0.

2 Likes

#5

Hi @dineshsubramani thank you for your attempt but as @kennyb6 said this is not what I was looking for. I want something like if the value is at list X in position 1 you can punt the second value at the end of that list. @kennyb6 saw what I mean

0 Likes

#6

Hi @Kulkul unfortunately this is not what i was looking for. See what @kennyb6 said to @dineshsubramani to see what I meant.

0 Likes

#7

I can’t test it right now for you but this should work in a python node, as long as that is your specific list structure:

l1 = IN[0] #list 1
l2 = IN[1] #list 2
for _l2 in l2:
    i = [l[0] for l in l1].index(_l2[0])
    l1[i].append(_l2[1])
OUT = l1
0 Likes

#8

Hi @kennyb6 unfortunately it is now working, maybe some typos but I don’t know what

0 Likes

#10

Slight modification to @kennyb6’s work:

L1 = IN[0]
L2 = IN[1]
L1_inds = [sublist[0] for sublist in L1]
for L2_sublist in L2:
    L2_ind = L2_sublist[0]
    if L2_ind in L1_inds: # Only proceed if matching index is found
        L2_val = L2_sublist[1]
        i = L1_inds.index(L2_ind)
        L1[i].append(L2_val)
OUT = L1

This will only create the list of first items in L1 once (rather than recreate it every loop) and will also only call list.index() if the index from L2 is actually in L1 (which would otherwise throw an exception). Would you be able to provide additional screenshots of what isn’t working?

1 Like