PDA

View Full Version : [PYTHON] Remove duplicate from array


Luckycat
11-25-2010, 04:08 PM
Hi,

I'm pretty new to python so sorry if this question looks too obvious for some of you...
I have an array with duplicate entries that I need to remove.
For example
array = ['blue','red','red','green']
needs to be
array = ['blue','red','green']
Is there any python function to do so ? Or do I have to loop inside the array ..
Thx for your help

Lc

Keilun
11-25-2010, 04:29 PM
You can use the python sets module. Those are essentially unordered collections that enforce uniqueness among its members.

Eg.

# DISCLAIMER: Not actually tested code
import sets
array = ['blue','red','red','green']
uniquearray = sets.Set(array)


Should result in what you want. For more info:

http://docs.python.org/library/sets.html

NaughtyNathan
11-25-2010, 04:42 PM
sets are builtin now Keilun...

array = list(set(array))However, this is a very interesting blog post on the whole subject:

http://www.peterbe.com/plog/uniqifiers-benchmark

:nathaN

Keilun
11-25-2010, 04:45 PM
Ah right. I'm on an old version of python for my current project. :) That link is news to me! Nice find. Thanks!

Luckycat
11-26-2010, 09:53 AM
Thank you both for your answers, intersting reading.
@NaughtyNathan : you add the list function to reconvert the array from set to list ?
Thx again

Lc

NaughtyNathan
11-26-2010, 10:16 AM
Yes Lc, that's exactly right. of course you can omit this step, but you started with a list, and if you aren't constructing or adding anything else onto it it makes sense to end up with a list as you can do a lot more stuff to/with a list than you can to a set.

:nathaN

ewerybody
11-26-2010, 05:27 PM
hehe @nathan: I thought of the same posting! So you actually got it! You didn't reply when I sent you that.:thumbsup:

So which function do you prefer now?

sagunmanandhar
01-07-2011, 09:36 PM
how to do with mel for int/float array ?

NaughtyNathan
01-08-2011, 01:20 AM
don't double post

http://forums.cgsociety.org/showthread.php?p=6824965#post6824965

EightBit
01-09-2011, 04:25 PM
You can also use a dictionary:
listOfLights = ['keyLight', 'rimLight', 'backLight', 'fillLight', 'rimLight']
listOfLights
listOfLights = dict.fromkeys(listOfLights) #Convert to a dictionary
listOfLights # Now a dictionary
listOfLights = listOfLights.keys() # Convert back to a list
listOfLights

CGTalk Moderation
01-09-2011, 04:25 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.