Merging two sub lists by their index


#1

Hey everyone,

I’m trying to merge two lists together and I’m having major trouble getting the desired results. I’m trying to replace items in a sublist by their index, with items from a seperate list (it’s sublist) by their index. I made a basic script to show you what I mean. I’m thinking maybe I need to use list map, but after playing around with that and feeding it various functions with bad results, I’m not entirely sure. Thank you!


#2

I’m thinking you can do this, I simply have no idea by what kind of logic you’re trying to merge these. The “0A” and alike seems to be thrown in pretty randomly at second and fourth position, but on what basis do you want them there? what happens to the “0B”?


#3

@PauLtus makes a good point. While it is possible, we need more info to give you a proper response. When needing to replace multiple items in the same list, I always find python the best solution. However, there are a few custom nodes that do this very well. ReplaceItemAtIndex+ from Clockwork is likely what you want here as it allows you to use a negative index (and therefore skip an item.)


#4

You’re right.
A lot of Clockwork nodes are actually pretty simple Python nodes in the first place.


#5

Thanks for the replies so far. lol… I tried to simplify my problem by making a fake example, but I guess that was not helpful. Preferably I would like to use Python (my existing code is attached. I’m very new to Python, so go easy on me!) or out of the box nodes.

Explanations…
Bom List - This is a simple list of unique product types
Mat List - This is a list with product types, task numbers, ship to code, job number, etc
(For the first part of the script, I need to go through the Bom list 1 item at a time, and if the product type in the bom list matches the product type in the mat list, pull ship to code (index 5 of mat list) and task number (index 4 of mat list) from that sublist. The other values in the mat list, I don’t care about)

Main List - This is the final list I’m working with. I need to insert the two values pulled above (ship to code and task number) back into this list at the same place as it is in the bom list. (Index 0 in the Bom list is list #102 of the Mat List. Pull index 4 and 5 from the Mat List #102 (2355 and SVD) and insert them in list 0 of the Main List) then find the next match, do the same, and so on.
Ship Index (6) - The index in the Main List where I need to insert each ship to code pulled from the Mat list sublists (so in list 0 of the main list, insert 2355 in index 17)
Task Index (17) - The index in the Main List where I need to insert each task number pulled from the Mat list sublists (so in list 0 of the main list, insert SVD (Valders due to the replace in the python code) in index 6)

So for each sub list of the main list, I need to insert the values pulled from the Mat List into index 6 and 17

This is where the Python script is not working as intended, nothing happens and I’m not sure what to put here:
main_list[count][task_index] = task_value
main_list[count][ship_index] = ship_value
From what I’ve read, in Python it’s best to create a new list instead of trying to replace items like I’m attempting to do. But I’m kinda stuck at this point (hence my reaching out to you amazing ppl) and my Python skills are sorely lacking. But I’m trying to get better.

Hopefully that makes sense! I’m not always good at explaining things.

Python Code below.

Amber

Code:

import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

# Import the lists and indexes
bom_list = IN[0]
mat_list = IN[1]
main_list = IN[2]
ship_index = IN[3]
task_index = IN[4]

# Loop through bom list
for bom in bom_list:

    # Begin counter
    count = 0

    # Loop through materal list
    for mat_bom in mat_list:
	
	    # If items match
	    if bom in mat_bom:
		
		    # Get index 4 and 5
		    task_value = mat_list[count][4]
		    ship_value = mat_list[count][5]
		
		    # Replace ship value (org code)
		    if ship_value == "SVD":
			    ship_value = "Valders"
		    if ship_value == "SCL":
			    ship_value = "Crystal Lake"
			
		    main_list[count][task_index] = task_value
		    main_list[count][ship_index] = ship_value	
			
	    # Increase counter by 1
	    count = count + 1


# DEBUG, uncomment lines below to use
stdsave = sys.stdout
fout = open(r'C:\Users\admin\desktop\output.txt','w')
sys.stdout = fout
print "Count: ",count
print "Task number: ",task_value
print "Ship to: ",ship_value
print "Task index: ",task_index
print "Ship index: ",ship_index
print "BOM: ",bom
sys.stdout = stdsave
fout.close()

# Output
OUT = main_list

#6

I’m leaving for the weekend soon so I don’t have time to look into your code right now, but assuming you can get your data in the same format as your example, you should be able to use the Clockwork node (or even better, pull it apart and use the python code as a starting point for your code).


#7

@apanz
is it something like this?
List Index (2 nodes).dyn (10.6 KB)


#8

Thanks Nick. I will check out their node and see if I can integrate it with my python script.


#9

Appreciate the effort, but this hurts my brain. :frowning: I’m not sure what I’m looking at here since everything is number or letter.


#10

@apanz,
Didn’t you ask for a node that can change item by their index at any depth?
My nodes support that since they are coded to run recursively in any nD list.

The image (and .dyn) file did I find from my local “sandbox” when I developed the two nodes. I cant remember when I revealed them the first time, but at that time I used the presented solution as a proof of concept.


#12

Tried the DanEDU package, but I’m just getting nulls. I tried with and without using levels on my main list. Same thing works in Clockwork’s, and we already have that package on our machines, so I suppose I’m going to use that. Thank you though.


#13

There was an error in the build I uploaded last night (2018.301.1) the version 2018.301.0 worked and i have uploaded 2018.301.2 without errors hopefully.

If Clockwork fixed your problem, then it is also fine…