[Python-ideas] list…pushed, or something

Shane Green shane at umbrellacode.com
Fri Mar 29 05:55:17 CET 2013


And I was suggesting something along these lines would be generally useful (if not pointlessly easy): 

class Set(set):
    __slots__ = ()
    def added(self, value): 
        super(Set, self).add(value) 
        return value

def unique(items):
    seen = Set()
    return (seen.added(item) for item in items if item not in seen)






On Mar 28, 2013, at 8:15 AM, MRAB <python at mrabarnett.plus.com> wrote:

> On 28/03/2013 05:28, Bruce Leban wrote:
>> 
>> On Wed, Mar 27, 2013 at 10:11 PM, Shane Green <shane at umbrellacode.com
>> <mailto:shane at umbrellacode.com>> wrote:
>> 
>>    [seen.added(value) for value in sequence if value not in seen]  *
>> 
>> 
>> Here's an easy way to do it:
>> 
>> >>> seen = set()
>> >>> seq = [3,2,1,2,3,4,5,4]
>> >>> [seen.add(v) or v for v in seq if v not in seen]
>> [3, 2, 1, 4, 5]
>> >>> seen
>> {1, 2, 3, 4, 5}
>> 
> I think I would prefer a "unique" function that yields unique items:
> 
> def unique(items):
>    seen = set()
> 
>    for item in items:
>        if item not in seen:
>            seen.add(item)
>            yield item
> 
> >>> seq = [3,2,1,2,3,4,5,4]
> >>> list(unique(seq))
> [3, 2, 1, 4, 5]
> 
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130328/b053d142/attachment.html>


More information about the Python-ideas mailing list