How to match the names in the list with the corresponding numbers?

Hi everyone,

I have a question to Dynamo.
I need to create from list with elements, another list with correspondiing number. Wery important is that the list number must be always from 1 to element numbers in list (1,2,3,4,5 if there is 5 elements)

Let us assume such names in the first list:

“sun”,“moon”,“sky”,“star”,“space” - that is the main order

there is always that names but sometimes ther are all sometimes only tree etc

Example:

list0:
(sun,sky,moon,moon,space) - the lsit i need is (1,4,2,3,5)
list1:
(moon,space,star) - (1,3,2)
list3:
(sun,sun,space,space,moon) - (1,2,4,5,3)

I know that it is probably only possible in python or some difficult codeblock but I dont know ho to make it. Please help.

Thanks in advance.

A dictionary should make short work of this.

Note that your keys need to be strings, so you might want to get the Mark from the elements, or otherwise convert them into unique string values for both the main order and the lookup lists.

1 Like

it is not exactly what I want because i need to have always with all list from 1 to element amound in list

Not sure I am following… “Moon” was the second item in the list, why should it return 1?

I mean it always must be 1,2,3,4… but in the order given above relative to these names: moon…

So the first number should be 1 in all cases?

The first number by from the order : “sun”,“moon”,“sky”,“star”,“space” - that is the main order
is is not a sun in list then moon gets 1

Still not quite sure I get you, but it may be this:

This sort of unique list indexing issue is often a case of ‘there is a better route’ but we’d need to see more of your workflow to know for sure.

1 Like

I agree with Jacob. You’re essentially “ranking” elements instead of sorting by discrete value. If you need to maintain a list order (which it seems like you do) it would likely be easier to reorder the controlling list than make this one match it.

This might be what you’re after… Nick’s use of “ranking” triggered something for me. :rofl:

2 Likes

"Wow, really, It is almost what I need. Thank you so much. I was trying all day in Python and got nothing, but you almost solved what I need to get. Maybe you now know how to get further with this and in the same order creaete more. Because now when there are two or more identical elements in a list, like moon, moon, sun, sun, sun, I need assign another number(In this example = 4,5,1,2,3) I think it will be really hard to accomplish this now, but thanks.:slight_smile:

It’s certainly possible. It’s just a lot of converting to indices and maintaining parallel lists to go back and forth between values and ranks - likely for something that could be simplified.

This is sounding more and more like a bad idea…

If you had [“moon”,“moon”,“sun”,“sun”,“sun”] what would you expect? Note that the triplicate sun is now going to return a value of 3, but moon is also a value of 3 so it’s now at 4… any relation to your original list is long gone.

That said the ‘perhaps add this’ did just what you’re asking for.

My understanding is that the list would represent indices (or count really) based on ordered value. Moon is the third ranked item. It doesn’t represent a value of 3. All objects of rank 1 and 2 would be counted before it.

Right now I get a good list with all i need butt when i have for example tree time moon i get 2,2,2 but i need get another number after sun like 2,3,4 if sun was only one in list

So you want this: How to match the names in the list with the corresponding numbers? - #8 by jacob.small

yes that was first step, now I need to make if the elements are repeat themselves give them another number by ofcourse order form first point

I’m sure it’s possible to do it with nodes but it’s too early to deal with that much list maintenance.
The python could probably be refined as well, but it works.

2 Likes

That is what i want. Than you very much. But i dont know why i get list
multiplied by 7 i should get 614 element but gets 4298(in this pythone node) ? :slight_smile:

Your append() line is indented incorrectly. It should be under the top loop.

2 Likes