Replace items in sub-lists


#1

I’m trying to replace all instances of a set value (in the image ‘1’) with a sequential letter. For a single list it worked fine but as soon as I add in sublists I get the problem that it replaces all the 1’s in the first list with A second list B etc. rather than maintaining the sequence.

I thought I could use a List Map to overcome the problem but that just returns a list of nulls…

Can anyone tell me where I’m going wrong?


#2

List.Map is quite tricky. Normally it´s better to use the level selector in the input ports. Anyway, ReplaceByCondition doesn’t give you the chance (and somehoy that might be rhe reason for List.Map not working). You could try either of these:


#3

Thanks @Joan_Moreto that’s interesting approach. The trouble is I need to go back to A again for each sub list. Apologies if I didn’t make that clear.


#4

You can try to turn the numbers to string, use String.Replace (which allows you to use the level selector) and then turn them back to numbers. Something like this


#5

Okay, that’s closer, the only difficulty here is that it’s effectively assigning a letter to each index so in your example I get A, 2, 2, D rather than A, 2, 2, B, etc.

I can live with all the data being strings if that makes things easier…

I’ve also just realised that my original solution that I thought worked on single level lists also exhibits this behaviour which is annoying! :o)


#6

You could try to somehow mix that with the second option in my previous post which solved that issue…


#7

This should do it. you need to utilise the Clockwork List.ReplaceAtIndex node as the core Dynamo node won’t give you the results.


#8

That’s much better than my solution :slight_smile:


#9

Thankyou both -

Joan, I was working on the train last night so didn’t get a chance to reply to your last post, I was trying to utilise your second solution but by extracting the 1’s and then using Replace Item - I’m still going to try and work this through to try and progress my own knowledge.

Scott - I’ll give that a shot - it’s frustrating when the core nodes don’t do quite what you expect - it would probably be good if some of the Clockwork stuff etc got incorporated as core modules.

Cheers

K.


#10

def rep(lst:var[],alp:var[])
{
return=[Imperative]
	{
	c=0;
	d=0;
		while (c<List.Count(lst) && d<List.Count(alp))
		{
			if (lst[c]==1)
			{
			lst[c]=alp[d];
			c=c+1;
			d=d+1;
			}
			else
			{
			lst[c]=lst[c];
			c=c+1;
			}
		}
	return=lst;
	}
};

#11

Hi All,

I have a similar problem.
I’m trying to replace items or sublists with other items/sublists. I found a lot of topics dicussing the same issue but I couldn’t find a solution, so please help me. Okay, here comes:

I have a list (Level 4) containing 14 sublists (Level 3) with "Roads"
These 14 sublists contains sublists (Level 2) with “Road_contours”.
(So for example: “Highway” contains 1 sublist “Highway_contours”; List “Parking places” contains 10 sublists “Parking places_contours”)
Al these contours exists out of sublists (Level 1) with lines and arcs

“A”: I know how to create a list with startpoints of the lines and arcs.
“B”: I know how to create a list with start and midpoint for the arcs,

But now comes my problem:
How do I replace the startpoints of the arcs of point 1 with the list with start and midpoints from “B”. (or add midpoints to list “A” under the sublist with startpoints of the arcs) I already did a chop.list to create sublists for every line/startpoint so I can replace “B” with “A”.
I also used List.AllIndicesOf to find the index nr of the arcs, so I could you use these Index to replace points from B in A.
It just doesn’t work, because there are to many sublists and I think Dynamo can’t work that deep without the use of List.map.

So I think I have to do something with List.map and/or List.combine and list.Replace.ItemAtIndex, but I can’t figure out how.

Please help me.

kind regards,
MD360


#12

Hi,

You can look at my post here (Replace By condition (list and sublist)),

I had the same problem and I found a solution. Maybe, It will help you.


#13

Did you try the Insert node also?
http://dictionary.dynamobim.com/#/BuiltIn/Action/Insert(list_var[]..[]-element_var-index_int)
With the last version of this node, you will be able to play with levels. If it does not work as such, you could try to apply Replication Guides in the Insert function in a code block. See:
http://dynamoprimer.com/en/07_Code-Block/7-2_Design-Script-syntax.html



Edit: I couldn’t quickly find an example so I made one: