I have a list with elements (sometimes letters, sometimes coordinates). I want to list the index of the duplicate elements. The “sort duplicate values” component does not work for this case because I need to retrieve the duplicate element index or null for each element in the same order that the original list is.
In other words, I would like the list to look like the attached image (null values are important to me).
I think it could be solved with a ghpyton script, but I’m starting to learn it.
Thanks Matt , but it´s not what I’m looking
Maybe I didn’t explain it clearly. The items of my list are sometimes letters and sometimes coordinates, but not both in the same list (sorry for the confusion). Some items in the list are duplicated. In the example I posted, the 0 index has the element C and it’s the same element stored in the 4 index. In the result I want to have the 0 index storing the number 4 that is the index of the duplicated element, but also I want to have the 4 index storing the number 0. The elements that aren’t duplicated in the list should appear like “null”
Yep, this is a good use for a few simple lines of scripting:
mapping = [None] * len(x)
for i, elem in enumerate(x):
try:
j = x.index(elem, i + 1) # look ahead
mapping[i] = j
mapping[j] = i
except ValueError: # no match found
pass
a = mapping
private void RunScript(List x, int index, ref object A)
{
var groupByOneX = x.Select((o, i) => new { o, i }).ToLookup(a => a.o, a => a.i)[x[index]].ToList();
groupByOneX.Remove(index);
A = groupByOneX;
}
Ah, I forgot to mention that x should be set to List Access (via right click menu) - the default behaviour runs the component N times for each value in the input.