List replace by condition (list of items to substitute)

Hi there,
I have a list which is populated by placeholder text and I wish to replace such text by other values later in the script.
I followed this script by @Dimitar_Venkov and I succeed with a single replacement item.
The point is now I have a list of items to replace and I wish to substitute one of them at the time.
I mean: 1st occurrence shall take 1st item on my “replacement list”, 2nd occurrence shall take 2nd item of my “replacement list” and so on.

I post a picture of my graph for better understanding, hoping someone could shed a light.
The code block shown the “static” replacement list which works feeding the same value for all the occurrencies.
On the left, the list.transpose contains the actual values of my “replacement list” I want to replace.


Thanks in advance.
Cheers.

Hi @paolo.pozzoli

Did you try replacing with dictionary?

1 Like

Hi @Kulkul and thanks for your reply.
Your solution is somehow over my head though and I couldn’t modify your code on my own to get what I need.

In my poor understanding I thought I need to loop through my original list to spot each occurrency, get the index and replace it with the value from the other list.
The ValuesList index shall be selected via a variable referring to the count the occurrencies found in the original list and incrementing as the loop gets executed.

Can you give me further support in this?
Thanks again for your time.

Hi @Kulkul, Hi forum,
I tried with my approach but I’m stuck with my Python code (see image below).
Can you please provide me support?
Thank you.

My code
# Load the Python Standard and DesignScript Libraries
import sys
import clr
clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *

# The inputs to this node will be stored as a list in the IN variables.
changeList = IN[0]
origList = IN[1]

# Place your code below this line
outList = origList
changeIndex = 0

for i in outList:
	if outList[i][0] == "CodArt":
		for subNumber in i:
			outList[i][subNumber] = changeList[changeIndex][subNumber]
#		outList[i][0] = changeList[changeIndex][0]
#		outList[i][1] = changeList[changeIndex][1]
#		outList[i][2] = changeList[changeIndex][2]
#		outList[i][3] = changeList[changeIndex][3]
			changeIndex += 1

# Assign your output to the OUT variable.
#OUT = outList[0][0], changeIndex
#OUT = outList[1][0], changeIndex, changeList[1][0]
OUT = outList

Update: I changed the code above as follows.
It works in python console but fails inside dynamo python. Can’t tell how.

example origList =[[“CodArt”,“Description”,“MU”,“Qty”],[“A”,“B”,“C”,“D”],[“E”,“F”,“G”,“H”],[“CodArt”,“Description”,“MU”,“Qty”]]

#example changeList =[[“CODE1”,“DESC1”,“UM1”,“QTA1”],[“CODE2”,“DESC2”,“UM2”,“QTA2”]]

changelist = IN[0]
origList = IN[1]

outList = origList
changeIndex = 0

for i in outList:
#print(outList[outList.index(i)][0])
if outList[outList.index(i)][0] == “CodArt”:
for subNumber in i:
outList[outList.index(i)][i.index(subNumber)] = changeList[changeIndex][i.index(subNumber)]

    changeIndex += 1

Assign your output to the OUT variable.

OUT = outList

Hi @paolo.pozzoli

It works in Dynamo also:

You have typing error:

2 Likes

Hi @Kulkul and huge apologies for the late reply.
I’ve been off for a while.
Thanks for your hint, I figured it out myself.

Hope you and the forum had a Merry Christmas and are enjoying the holidays.
Bye

1 Like