Lacing on replacing item on index

#6

Nick,

Yes, that is exactly what I am trying to do! unfortunately that I dont have any python skills. Are there any packages out there that can do this? it seems like it would be a fairly common issue. no? Thanks

@erfajo
Thanks, but what I am really trying to do is:
image

0 Likes

#7

Clockwork has ReplaceItemAtIndex+ that does just this. Or you can look at this Python code that does the same thing.

dataEnteringNode = IN
list = IN[0]
elements = IN[1]
indices = IN[2]
count = 0
i = 0
out = []
for item in list:
	try:
		if count == indices[i]:
			out.append(elements[i])
			i = i+1
			count = count+1
		else:
			out.append(item)
			count = count+1		
	except:
		out.append(item)
		count = count+1

OUT = out
3 Likes

#8

@Nick_Boyts
Jackpot! Thanks Nick!
image

0 Likes

#9

Andreas actually uses a much nicer method for replacing items.

dataEnteringNode = IN
list = IN[0]
elements = IN[1]
indices = IN[2]

for e,i in zip(elements,indices):
	list[i]=e

OUT = list

The simplicity of Python still amazes me at times.

1 Like

#10

Its not working for me. Do I have to flatten all the lists going into these python scripts?

0 Likes

#11

@Nick_Boyts your last one will go out of index… you need to pad

list1 = IN[0]
list2 = IN[1]
index = IN[2]

def pad(var, length):
    return var[:length] + [None]*(length - len(var))

pad(list1, len(list1) + len(list2) )
for idx, item in zip(index,list2):
    list1.insert(idx, item)

OUT = list1
1 Like

Python Nodes Basics
#12

Those are both setup for one list of elements with one one list of replacements. You’d have to modify the code a bit to take multiple lists.

0 Likes

#13

0 Likes

#14

Seems to work for me with a simple test case.
image

0 Likes

#15

yeah it did for me too. I just think its a flattening or a lacing issue

0 Likes

#16

that’s because you have the length defined before you start changing items… if you have a list of length 6 and want to add 4 items at specific index… you need to pad the list to size 10

I read the problem as… a list of 37 items that need 12 items to be added (at specific index), so the list end at 59 elements

0 Likes

#17

It’s hard to tell what the issue is when we can’t see any of your inputs. You might have more control using the node from Clockwork.

0 Likes

#18

Gotcha.
But for replacing items you should be fine as long as your indices are within range.

0 Likes

#19

Sorry,
Here you go its really hard with such a small screen:

0 Likes

#20

I am sure that will go wrong…
the list is of 37 elements and 12 to be added at specific index, not to be replaced… but i might read the question wrongly

0 Likes

#21

He was initially trying to add items at an index but that was after he had already removed the same indices. What he was really looking for was a replace at index but it wasn’t working.

0 Likes

#22

Yes you’ll need to flatten your list of rectangles. The list structure doesn’t match that of the indices (one index per element).

1 Like

#23

Thanks!

0 Likes

#24

you are probably right, I read the “insert” node as a wish to insert and not replace… so my mistake :slight_smile:
I can see now that it was replace that was the goal… but now there is a code for padding if someone can use that :slight_smile:

0 Likes

#25

It was a little confusing. I didn’t realize it until I looked at why his indices were out of range.

0 Likes