I have a List and want to insert multiple values on multiple indexes. It seems so easy but I dont get how to do it. And didnt find it in the forum.
Is there no node like List.PlaceItematindex?
With List.Insert it everytime takes the whole list. But I want to have the single values.
First time it should take first value, secont time second value and so on. How to do it?
Sorry, what I mean is that there are two ways to interpret inserting using list of items and list of indices.
Let’s say you wanted the value 3 to be inserted into the list in this example. Since it is the third item to be inserted, the items at index 9 has changed. Instead of being between 58 and 59 (original list), the new index 9 is between 56 and 57.
If you want to try it to see which works better, here is the python:
# Enable Python support and load DesignScript library
import clr
_list = IN[0]
elems = [x for _,x in sorted(zip(IN[2],IN[1]))]
keep = IN[3]
if keep:
indices = [v+i for i,v in enumerate(sorted(IN[2]))]
else:
indices = sorted(IN[2])
for ind, elem in zip(indices, elems):
_list.insert(ind, elem)
OUT = _list
I have only done a little bit of testing but it should be able to handle lists where the indices are not sorted as long as the items and indices are lined up.
The boolean True means that it will keep the original list index, meaning that the insert is based on the items, not the index. False is the opposite.
If you look inside the node you’ll see similar Python to Kenny’s but with a bit of error catching.
It’s worth considering if you would rather manage Python code yourself, or put it in a custom node (you can update the node once and it updates in all your graphs) or if you’d rather use a package that someone else will update for you.
The RIE does the other version of what mine does. I don’t have the package installed so I can’t say what exactly it does but it basically works like the other scenario I mentioned.
That said, @Mark.Ackerley is right that the Python is more difficult to maintain and will not carry over into other graphs like a package would. I also did not put in any error catching for if the two lists are of different sizes or other problems.
OUT = []
add = 0
if isinstance(IN[1], list):
ItemList = IN[1]
else:
ItemList = [IN[1]]
if isinstance(IN[2], list):
IndexList = IN[2]
else:
IndexList = [IN[2]]
if len(IN[0]) > 0:
if len(ItemList) == len(IndexList):
for _ in range(0,len(IN[0])+len(IndexList)):
if _ in IndexList:
OUT.append(ItemList[IndexList.index(_)])
else:
OUT.append(IN[0][add])
add += 1
else:
OUT = "Item list and Index list must be of equal length"
else:
NewItemList = [x for _,x in sorted(zip(IndexList,ItemList))]
for _ in NewItemList:
OUT.append(_)
If you don’t understand Python, you are probably better off using the one @Mark.Ackerley suggested. If the index is not in the right place, then you just need to add to the index list.