Re: insert unique data in a list
On 12월14일, 오전2시57분, mattia wrote:
> Il Sun, 13 Dec 2009 16:37:20 +, mattia ha scritto:
>
> > How can I insert non-duplicate data in a list? I mean, is there a
> > particular option in the creation of a list that permit me not to use
> > something like:
> > def append_unique(l, val):
> > if val not in l:
> > l.append(val)
>
> > Thanks,
> > Mattia
>
> Ok, so you all suggest to use a set. Now the second question, more
> interesting. Why can't I insert a list into a set? I mean, I have a
> function that returns a list. I call this function several times and
> maybe the list returned is the same as another one already returned. I
> usually put all this lists into another list. How can I assure that my
> list contains only unique lists? Using set does'n work (i.e. the python
> interpreter tells me: TypeError: unhashable type: 'list')...
this makes the set type hashable.
class Set(set):
__hash__ = lambda self: id(self)
s = Set()
s.add(1)
s.add(2)
s.add(1)
print s
set([1, 2])
d = {}
d[s] = 'voila'
print d
{Set([1,2]):'voila'}
print d[s]
'voila'
--
http://mail.python.org/mailman/listinfo/python-list
Re: insert unique data in a list
On 12월14일, 오전10시19분, knifenomad wrote:
> On 12월14일, 오전2시57분, mattia wrote:
>
>
>
>
>
> > Il Sun, 13 Dec 2009 16:37:20 +, mattia ha scritto:
>
> > > How can I insert non-duplicate data in a list? I mean, is there a
> > > particular option in the creation of a list that permit me not to use
> > > something like:
> > > def append_unique(l, val):
> > > if val not in l:
> > > l.append(val)
>
> > > Thanks,
> > > Mattia
>
> > Ok, so you all suggest to use a set. Now the second question, more
> > interesting. Why can't I insert a list into a set? I mean, I have a
> > function that returns a list. I call this function several times and
> > maybe the list returned is the same as another one already returned. I
> > usually put all this lists into another list. How can I assure that my
> > list contains only unique lists? Using set does'n work (i.e. the python
> > interpreter tells me: TypeError: unhashable type: 'list')...
>
> this makes the set type hashable.
>
> class Set(set):
> __hash__ = lambda self: id(self)
>
> s = Set()
> s.add(1)
> s.add(2)
> s.add(1)
>
> print s
> set([1, 2])
>
> d = {}
> d[s] = 'voila'
>
> print d
> {Set([1,2]):'voila'}
>
> print d[s]
> 'voila'- 원본 텍스트 숨기기 -
>
> - 원본 텍스트 보기 -
although it's not what you've asked about. it's intereting to make set
hashable using __hash__.
--
http://mail.python.org/mailman/listinfo/python-list
Re: insert unique data in a list
On 12월14일, 오후12시42분, Steven D'Aprano
wrote:
> On Sun, 13 Dec 2009 17:19:17 -0800, knifenomad wrote:
> > this makes the set type hashable.
>
> > class Set(set):
> > __hash__ = lambda self: id(self)
>
> That's a *seriously* broken hash function.
>
> >>> key = "voila"
> >>> d = { Set(key): 1 }
> >>> d
>
> {Set(['i', 'a', 'l', 'o', 'v']): 1}>>> d[ Set(key) ]
>
> Traceback (most recent call last):
> File "", line 1, in
> KeyError: Set(['i', 'a', 'l', 'o', 'v'])
>
> --
> Steven
of course it is broken as long as it uses it's instance id.
i added this to notify that unhashable can become hashable
implementing __hash__ inside the class. which probably set to None by
default.
--
http://mail.python.org/mailman/listinfo/python-list
any modules having a function to partition a list by predicate provided?
i know it's not very hard to get that solution. just by implementing simple function like below. def partition(target, predicate): """ split a list into two partitions with a predicate provided. any better ideas? :) """ true = [] false= [] for item in target: if predicates(item): true.append(item) else: false.append(item) return true, false but i wonder if there's another way to do this with standard libraries or .. built-ins. if it's not, i'd like the list objects to have partition method like string module has. true, false = [1,2,3,4].partition(lambda x: x >1) print true, false [2,3,4] [1] -- http://mail.python.org/mailman/listinfo/python-list
