Hi!
If I have an array like this:
{0,1,2,2,7,9,9,9,33}
…How can I remove all the same values to end up with:
(0,1,2,7,9,33)
?
-Olivier
array: remove same values?
9415 10 2- olivierth
- Member
- 1073 posts
- Joined: 4月 2017
- Offline
- bonsak
- Member
- 459 posts
- Joined: 10月 2011
- Offline
You could do it in python:
This example runs in a Python SOP and the geo is just one point with a detail array attribute
-b
geo = hou.pwd().geometry() my_list = geo.intListAttribValue('my_list') # Get the detail attribute no_duplicates = list(set(my_list)) # Remove duplicates geo.setGlobalAttribValue('my_list', no_duplicates) # Store the new values
This example runs in a Python SOP and the geo is just one point with a detail array attribute
-b
- olivierth
- Member
- 1073 posts
- Joined: 4月 2017
- Offline
- BabaJ
- Member
- 2125 posts
- Joined: 9月 2015
- Offline
Yes…in a wrangle set to detail:
//i[]@List = {4,5,8,8,2,3,0,1};
//i[]@List = {4,4,8,8,2,3,8,4,8};
i[]@List = {1,8,8,2,7,3,8,4,8,6,7,1,1};
int Count, Count_A;
for(Count = 0; Count < len(i[]@List); Count++)
{
for(Count_A = 0; Count_A < len(i[]@List); Count_A++)
{
if( (i[]@List[Count] == i[]@List[Count_A]) && (Count != Count_A) )
{
removeindex(i@List, Count_A);
}
}
}
- olivierth
- Member
- 1073 posts
- Joined: 4月 2017
- Offline
- BabaJ
- Member
- 2125 posts
- Joined: 9月 2015
- Offline
In case your interested…maybe if this is the only code in a wrangle that one is going to use…it's not too bad that it's ‘complicated’…but if your going to need to do other things in the same wrangle, what I like to do as in this case where I know as in this case, that this code is like to be used elsewhere, is to make it an ‘external’ function.
So with the above what you could do is copy and paste into a text file, with some modifications to make it like this:
Then save it as a text file with an extension of your preference…I like to use *.h as personal convention.
For my project, because I work as a ‘freelancer’ and not in a production team I tend to keep all files within/reference to the project $HIP directory.
In that directory I create a folder named ‘vex’ and within that folder another one named ‘include’ within that I place the above file/s with my .h extensions.
Then in any wrangle of my project file the first line is as follows:
and after that I only need to use one line to make use of the function which keeps it looking simpler to read and add more code/other functions:
Also, just to be clear of another convention I do…I tend to name the text file (with the .h extension) the same name as the function…for easy and quickly identifying my needs since if I want to use other functions I may have created is just a matter of adding additional #include lines:
So with the above what you could do is copy and paste into a text file, with some modifications to make it like this:
int[] Remove_Duplicate_Array_Items(int Imported_Array[])
{
int List[];
int Count, Count_A;
List = Imported_Array;
for(Count = 0; Count < len(List); Count++)
{
for(Count_A = 0; Count_A < len(List); Count_A++)
{
if( (List[Count] == List[Count_A]) && (Count != Count_A) )
{
removeindex(List, Count_A);
}
}
}
return List;
}
Then save it as a text file with an extension of your preference…I like to use *.h as personal convention.
For my project, because I work as a ‘freelancer’ and not in a production team I tend to keep all files within/reference to the project $HIP directory.
In that directory I create a folder named ‘vex’ and within that folder another one named ‘include’ within that I place the above file/s with my .h extensions.
Then in any wrangle of my project file the first line is as follows:
#include <Remove_Duplicate_Array_Items.h>
and after that I only need to use one line to make use of the function which keeps it looking simpler to read and add more code/other functions:
#include <Remove_Duplicate_Array_Items.h>
//i[]@List = {4,5,8,8,2,3,0,1};
//i[]@List = {4,4,8,8,2,3,8,4,8};
i[]@List = {1,8,8,2,7,3,8,4,8,6,7,1,1};
i[]@List = Remove_Duplicate_Array_Items(i[]@List);
Also, just to be clear of another convention I do…I tend to name the text file (with the .h extension) the same name as the function…for easy and quickly identifying my needs since if I want to use other functions I may have created is just a matter of adding additional #include lines:
#include <Remove_Duplicate_Array_Items.h>
#include <Another_One_Of_My_Functions.h>
#include <One_Thousand_Line_Function.h>
//Happy coding here;
- olivierth
- Member
- 1073 posts
- Joined: 4月 2017
- Offline
- Fele
- Member
- 36 posts
- Joined: 11月 2013
- Offline
I was just looking for vex solution and found this thread...
Just in case anyone else is looking for this, maybe here is another, shorter solution:
Another way could be to assign a value like "0" to the duplicate values instead of removing..
Just in case anyone else is looking for this, maybe here is another, shorter solution:
f[]@myarray = {1,1,1,2,2,3,4,5,5,5,6,7,7,7,8,8,9}; float current; float next; for(int i = len(@myarray); i > -1; i--) { current = @myarray[i]; next = @myarray[i-1]; if(current == next) { removeindex(@myarray, i); } }
Another way could be to assign a value like "0" to the duplicate values instead of removing..
Edited by Fele - 2021年9月29日 07:41:33
- evrstudio
- Member
- 5 posts
- Joined: 1月 2019
- Offline
- josyzhaocn
- Member
- 3 posts
- Joined: 3月 2019
- Offline
- OdFotan
- Member
- 192 posts
- Joined: 4月 2015
- Offline
-
- Quick Links