Re: Best way to make a list unique?

2005-03-11 Thread Paul Rubin
I usually do it by sorting the list and then scanning in the obvious way, moving stuff down, and doing a single del operation at the end. -- http://mail.python.org/mailman/listinfo/python-list

Re: Best way to make a list unique?

2005-03-09 Thread Michael Spencer
Marc Christiansen wrote: Michael Spencer <[EMAIL PROTECTED]> wrote: Nice. When you replace None by an object(), you have no restriction on the elements any more: Thanks for the suggestion, Marc. Note that if there is no need to access the middle of the collection, then the implementation is simpl

Re: Best way to make a list unique?

2005-03-09 Thread Marc Christiansen
Michael Spencer <[EMAIL PROTECTED]> wrote: Nice. When you replace None by an object(), you have no restriction on the elements any more: > Here's something to work with: > > class OrdSet(object): > def __init__(self, iterable): > """Build an ordered, unique collection of hashable ite

Re: Best way to make a list unique?

2005-03-09 Thread Kent Johnson
Delaney, Timothy C (Timothy) wrote: Diez B. Roggisch wrote: This is actually one thing that Java 1.5 has that I'd like to see in Python - the LinkedHashSet and LinkedHashMap. Very useful data structures. Implementing these is fairly simple. There are two Ordered Dictionary recipes in the cookbook a

Re: Best way to make a list unique?

2005-03-09 Thread Michael Hoffman
Delaney, Timothy C (Timothy) wrote: I've proposed this on python-dev, but the general feeling so far is against it. So far the only use case is to remove duplicates without changing order, and there are iterator-based solutions which would normally be preferable. I've needed a dict that would itera

Re: Best way to make a list unique?

2005-03-08 Thread Michael Spencer
Delaney, Timothy C (Timothy) wrote: Michael Hoffman wrote: For those who don't know, these implement a hash set/map which iterates in the order that the keys were first added to the set/map. I would love to see such a thing. I've proposed this on python-dev, but the general feeling so far is agai

RE: Best way to make a list unique?

2005-03-08 Thread Delaney, Timothy C (Timothy)
Michael Hoffman wrote: >>> For those who don't know, these implement a hash set/map which >>> iterates in the order that the keys were first added to the set/map. > > I would love to see such a thing. I've proposed this on python-dev, but the general feeling so far is against it. So far the only

Re: Best way to make a list unique?

2005-03-08 Thread Michael Hoffman
Steven Bethard wrote: Delaney, Timothy C (Timothy) wrote: For those who don't know, these implement a hash set/map which iterates in the order that the keys were first added to the set/map. I would love to see such a thing. -- Michael Hoffman -- http://mail.python.org/mailman/listinfo/python-list

Re: Best way to make a list unique?

2005-03-08 Thread BJörn Lindqvist
The Cookbook features another interesting way to do it: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204297 -- mvh Björn -- http://mail.python.org/mailman/listinfo/python-list

RE: Best way to make a list unique?

2005-03-08 Thread Delaney, Timothy C (Timothy)
Steven Bethard wrote: > Sounds like a good candidate for the collections module. Of course > someone will need to implement it. ;) I'll suggest it to Raymond ... ;) Tim Delaney -- http://mail.python.org/mailman/listinfo/python-list

Re: Best way to make a list unique?

2005-03-08 Thread Steven Bethard
Delaney, Timothy C (Timothy) wrote: This is actually one thing that Java 1.5 has that I'd like to see in Python - the LinkedHashSet and LinkedHashMap. Very useful data structures. For those who don't know, these implement a hash set/map which iterates in the order that the keys were first added to

RE: Best way to make a list unique?

2005-03-08 Thread Delaney, Timothy C (Timothy)
Diez B. Roggisch wrote: > No. But I doubt that that is what you actually want, as listA will > lose its order afterwards. Typically, something like that gets > written like this: This is actually one thing that Java 1.5 has that I'd like to see in Python - the LinkedHashSet and LinkedHashMap. Ve

Re: Best way to make a list unique?

2005-03-08 Thread Scott David Daniels
Max M wrote: Eric Pederson wrote: listA = list(Set(listA)) As of 2.4, set is a built-in type (2.3 had Set in module sets). Another 2.4-ism is "sorted", which might very well be the way you want to turn the set into a list: listA = sorted(set(listA)) for this particular use, you can define sorte

Re: Good variable names (Was: Re: Best way to make a list unique?)

2005-03-08 Thread Diez B. Roggisch
> I'm going to go off on a tangent here and put in a plea for better > variable > naming. I'm looking at the above code, and can't figure out what "res" is > supposed to be. Is it short for "rest", as in "the rest of the items"? > Residual? Result? Restore? Any of these seems plausable. Not k

RE: Best way to make a list unique?

2005-03-08 Thread Batista, Facundo
Title: RE: Best way to make a list unique? [Max M] #-  >>> la = [1,2,3,4,3,2,3,4,5] #-  >>> from sets import Set #-  >>> sa = Set(la) #-  >>> for itm in sa: #- ... print itm Remember that in Python 2.4 you have ´´set´´ as a built-in data type:

Good variable names (Was: Re: Best way to make a list unique?)

2005-03-08 Thread Roy Smith
"Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > inserted = set() > res = [] > for e in listA: >if not e in inserted: >res.append(e) >inserted.add(e) > listA = res I'm going to go off on a tangent here and put in a plea for better variable naming. I'm looking at the above code

Re: Best way to make a list unique?

2005-03-08 Thread Max M
Eric Pederson wrote: I have listA=[1,2,3,4,5,4,3,4,3,2,1] and I want a list of only the unique members. This seems inefficient, but works fine over my small sample lists: listA=[a for a in set(listA)] Is there a more efficient approach for cases where listA is large? no. Even though the code

Re: Best way to make a list unique?

2005-03-08 Thread Diez B. Roggisch
Eric Pederson wrote: > I have > listA=[1,2,3,4,5,4,3,4,3,2,1] > > and I want a list of only the unique members. > > This seems inefficient, but works fine over my small sample lists: > listA=[a for a in set(listA)] > > > Is there a more efficient approach for cases where listA is l