Hi all, my brain is completely fried at the moment, so bear with me.
I have a list with multiple duplicate values that I want to make unique by adding a 1,2,3,… sequence at the end of the value.
A lot of the solutions on here say that the way to go is to group by key but that would mess up my original list order which I want/need to keep.
I’m open to any other suggestions other than to group by key.
If it’s not possible I’ll have to rework the majority of my script which I would prefer not to do since my brain is already fried and I’m in a time crunch…
I’ve recreated my values in a seperate script without sharing my full graph:
Add sequence.dyn (12.6 KB)
My gut says there is probably a solution with python but unfortunately I have not found the time yet to deepdive into python…
one way to accomplish this, line 6 could be slightly confusing we could discuss it further some other time if u r interested:
Add sequence.dyn (14.4 KB)
def track_occurences(lst):
counter = {}
output = []
for x in lst:
counter[x] = counter.get(x, 0) + 1
output.append(x+str(counter[x]))
return output
OUT = track_occurences(IN[0])
1 Like
Thanks! This is awesome!
Would this still work if there are no duplicate values? As in, would it add only a ‘1’ if it’s already a unique value?
yeah i think so. credits to line 6.
1 Like
Hi, I was going in a different direction, but how can we sum terms of terms in sublists?
You can probably guess that I’m not very good at Python.
(I almost have to know the number of unique items.)
import sys
ll=IN[0]
def addcum(l):
c=0
rl=[]
for i in range (1,len(l)+1):
if l[i-1]==0:
c=c+0
rl.append(0)
else:
c=c+1
rl.append(c)
return rl
cum=[addcum(l) for l in ll]
OUT = [str(i+j+k) for i,j,k in zip(cum[0],cum[1],cum[2])],cum
Sincerely,
christian.stan
edit:
find here
https://stackoverflow.com/questions/18713321/element-wise-addition-of-2-lists
1 Like
unpacking operator should be helpful. but im fascinated by the fact u made a binary mask. im thinkin numpy/pandas could have some one liners to solve op’s problem.
1 Like
Not yet at the level sufficient to understand all the words but I hope one day, thanks again 
cordially
christian.stan
and thank u! first looked at op’s question, i didn’t think of approaching it the way u did. nice work as always!
1 Like
Not necessarily. You also group the indices simultaneously to track their positions.
lst1 = List.GroupByKey(lst0,lst0)["groups"];
lst2 = List.GroupByKey(1..List.Count(lst0),lst0)["groups"];
lst3 = lst1 + (1..List.Count(lst1<1>));
List.SortByKey(List.Flatten(lst3,-1),List.Flatten(lst2,-1))["sortedList"];
sfx.dyn (23.3 KB)
3 Likes
I mean… if @Vikram_Subbaiah is going to go design script with it…
Here’s a sample design script definition which just iterates over the list, appending an incrementing number to each item in the list until it gets a unique value for all items. All items are sequential so order isn’t of consequence. Execution time seems fairly good in my testing, but admittedly I didn’t try 10000000 item deep lists.
def uniquifyStrings (lst: string[])
{
newList = [Imperative]
{
myList = [];
for (i in lst)
{
noFit = true;
numb = 1;
orig = i;
while (noFit)
{
if (List.Contains(myList,i))
{
i = orig+numb;
numb = numb+1;
}
else
{
myList = List.Join([myList,i]);
noFit = false;
}
}
}
return myList;
};
return newList;
};
3 Likes
OUT = [v + str(IN[0][:i].count(v)) for i, v in enumerate(IN[0], 1)]
4 Likes
Same idea with OOTB Nodes
2 Likes