Is there a unique method in python to unique a list?

John H. Li typetoken at gmail.com
Sun Sep 9 02:29:02 EDT 2012


Many thanks.

I put all the set result into a list first . Then it will work and work
without result displayed.
>>> import nltk
>>> from nltk.corpus import wordnet as wn
>>> def average_polysemy(pos):
synset_list = list(wn.all_synsets(pos))
sense_number = 0
lemma_list =[]
for synset in synset_list:
lemma_list.extend(synset.lemma_names)
unqiue_lemma_list = list(set(lemma_list))
for lemma in unique_lemma_list:
sense_number_new = len(wn.synsets(lemma, pos))
sense_number = sense_number + sense_number_new
return sense_number/len(unique_lemma_list)

>>> average_polysemy('n')

However, if I don't put  list(set(lemma_list))  to a variable name, it
works much faster. Say:

>>> def average_polysemy(pos):
synset_list = list(wn.all_synsets(pos))
sense_number = 0
lemma_list = []
for synset in synset_list:
lemma_list.extend(synset.lemma_names)
for lemma in list(set(lemma_list)):
sense_number_new = len(wn.synsets(lemma, pos))
sense_number = sense_number + sense_number_new
return sense_number/len(set(lemma_list))

>>> average_polysemy('n')
1

Do you know why there is such a big difference? Does that mean set() will
work slower if its value is given to a variable name?


On Sun, Sep 9, 2012 at 1:59 PM, Donald Stufft <donald.stufft at gmail.com>wrote:

>  seen = set()
> uniqued = []
> for x in original:
>     if not x in seen:
>         seen.add(x)
>         uniqued.append(x)
>
> or
>
> uniqued = []
> for x in oriignal:
>     if not x in uniqued:
>         uniqued.append(x)
>
> The difference between is option #1 is more efficient speed wise, but uses
> more memory (extraneous set hanging around), whereas the second is slower
> (``in`` is slower in lists than in sets) but uses less memory.
>
> On Sunday, September 9, 2012 at 1:56 AM, John H. Li wrote:
>
> Many thanks. If I want keep the order, how can I deal with it?
> or we can list(set([1, 1, 2, 3, 4])) = [1,2,3,4]
>
>
> On Sun, Sep 9, 2012 at 1:47 PM, Donald Stufft <donald.stufft at gmail.com>wrote:
>
>  If you don't need to retain order you can just use a set,
>
> set([1, 1, 2, 3, 4]) = set([1, 2, 3, 4])
>
> But set's don't retain order.
>
> On Sunday, September 9, 2012 at 1:43 AM, Token Type wrote:
>
> Is there a unique method in python to unique a list? thanks
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120909/cb3f0eff/attachment.html>


More information about the Python-list mailing list