Re: Sets in Python

2007-09-22 Thread sapsi
Thank you everyone for the assistance and for the very informative discussion Regards SM -- http://mail.python.org/mailman/listinfo/python-list

Re: Sets in Python

2007-09-21 Thread Bryan Olson
Gabriel Genellina wrote: > En Thu, 20 Sep 2007 08:46:29 -0300, Steven D'Aprano > >> Another way is to use this class: >> >> class HashableList(list): >> def __hash__(self): >> return hash(tuple(self)) > > ...and that will stop working as soon as the list is mutated (which is > exact

Re: Sets in Python

2007-09-21 Thread Gabriel Genellina
En Thu, 20 Sep 2007 08:46:29 -0300, Steven D'Aprano <[EMAIL PROTECTED]> escribi�: > Another way is to use this class: > > class HashableList(list): > def __hash__(self): > return hash(tuple(self)) ...and that will stop working as soon as the list is mutated (which is exactly what

Re: Sets in Python

2007-09-20 Thread Terry Reedy
"Chris Mellon" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] | On 9/20/07, Dustan <[EMAIL PROTECTED]> wrote: | > On Sep 19, 10:58 pm, Bryan Olson <[EMAIL PROTECTED]> wrote: | > > Bad news: Python 3000 has no immutable type for byte-strings. | > > The new bytes type cannot serve for

Re: Sets in Python

2007-09-20 Thread Chris Mellon
On 9/20/07, OKB (not okblacke) <[EMAIL PROTECTED]> wrote: > Steven D'Aprano wrote: > > > But of course you can't look up the dict by value, only by > > identity. But that's what you wanted. > > Actually, if I understand the OP's examples right, he wants to look > up only by value, not by id

Re: Sets in Python

2007-09-20 Thread Chris Mellon
On 9/20/07, Dustan <[EMAIL PROTECTED]> wrote: > On Sep 19, 10:58 pm, Bryan Olson <[EMAIL PROTECTED]> wrote: > > Bad news: Python 3000 has no immutable type for byte-strings. > > The new bytes type cannot serve for dict keys or set members. > > Many things one would want to hash are unhashable -- fo

Re: Sets in Python

2007-09-20 Thread OKB (not okblacke)
Steven D'Aprano wrote: > But of course you can't look up the dict by value, only by > identity. But that's what you wanted. Actually, if I understand the OP's examples right, he wants to look up only by value, not by identity. -- --OKB (not okblacke) Brendan Barnwell "Do not follow whe

Re: Sets in Python

2007-09-20 Thread Neil Cerutti
On 2007-09-20, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: >> In the new model, it should be the value at the time of >> addition. That is [1,2] (not [1,2,3]). This does mean a copy >> of key in maintained internally in the dict. > > A copy!? That has to be a deep copy. Which would make >

Re: Sets in Python

2007-09-20 Thread Piet van Oostrum
> Steven D'Aprano <[EMAIL PROTECTED]> (SD) wrote: >SD> In other words, if you have two mutable objects M1 and M2, then you >SD> expect: >SD> hash(M1) == hash(M2) if and only if M1 and M2 are equal >SD> hash(M1) != hash(M2) if M1 and M2 are unequal Huh? Unequal things may hash to the same va

Re: Sets in Python

2007-09-20 Thread Steven D'Aprano
On Thu, 20 Sep 2007 04:02:03 +, Karthik Gurusamy wrote: > On Sep 19, 7:17 pm, Steven D'Aprano <[EMAIL PROTECTED] > cybersource.com.au> wrote: >> On Wed, 19 Sep 2007 20:58:03 +, Karthik Gurusamy wrote: >> > While it's easy to explain the behavior, I think the decision to dis- >> > allow mut

Re: Sets in Python

2007-09-20 Thread Dustan
On Sep 19, 10:58 pm, Bryan Olson <[EMAIL PROTECTED]> wrote: > Bad news: Python 3000 has no immutable type for byte-strings. > The new bytes type cannot serve for dict keys or set members. > Many things one would want to hash are unhashable -- for > example, the results of the hash functions in hash

Re: Sets in Python

2007-09-20 Thread Steve Holden
Karthik Gurusamy wrote: > On Sep 19, 7:17 pm, Steven D'Aprano <[EMAIL PROTECTED] > cybersource.com.au> wrote: >> On Wed, 19 Sep 2007 20:58:03 +, Karthik Gurusamy wrote: >>> While it's easy to explain the behavior, I think the decision to dis- >>> allow mutable items as keys is a bit arbitrary.

Re: Sets in Python

2007-09-20 Thread thebjorn
On Sep 20, 9:50 am, thebjorn <[EMAIL PROTECTED]> wrote: it's bad form to reply to myself, I know, but > def __iter__(self): > for k in super(mdict,self).__iter__(): > yield eval(k) should probably be def __iter__(self): return (eval(k) for k in super(mdict,se

Re: Sets in Python

2007-09-20 Thread thebjorn
On Sep 20, 6:02 am, Karthik Gurusamy <[EMAIL PROTECTED]> wrote: > On Sep 19, 7:17 pm, Steven D'Aprano <[EMAIL PROTECTED] > [...] > > (2) Allow the hash of mutable objects to change, which means you can use > > mutable objects as keys in dicts but if you change them, you can no > > longer find them

Re: Sets in Python

2007-09-20 Thread Marc 'BlackJack' Rintsch
On Thu, 20 Sep 2007 03:46:08 +, prikar20 wrote: > On Sep 19, 5:25 pm, Mark Dickinson <[EMAIL PROTECTED]> wrote: >> On Sep 19, 7:26 pm, Karthik Gurusamy <[EMAIL PROTECTED]> wrote: >> >> > If I did, a = [10, 20] and I did d[a]= 'foo', then a.append(30). >> > If dict complains key error on d[a] n

Re: Sets in Python

2007-09-19 Thread thebjorn
On Sep 20, 4:17 am, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: [...] > Data structures don't have problems. Programmers do. That's QOTW material :-) > ... And language > designers with sense build languages that minimize the programmers > problems, not maximize them. > ... > >

Re: Sets in Python

2007-09-19 Thread Paddy
On Sep 20, 5:02 am, Karthik Gurusamy <[EMAIL PROTECTED]> wrote: > In the new model, at the time of addition, you need to remember the > key at that time. If it's a list, you make a copy of the items. In other words you ask the dict to freeze any mutable keys given to it. Try an implementation and y

Re: Sets in Python

2007-09-19 Thread Karthik Gurusamy
On Sep 19, 7:17 pm, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: > On Wed, 19 Sep 2007 20:58:03 +, Karthik Gurusamy wrote: > > While it's easy to explain the behavior, I think the decision to dis- > > allow mutable items as keys is a bit arbitrary. There is no need for > > dict

Re: Sets in Python

2007-09-19 Thread Bryan Olson
Karthik Gurusamy wrote: > While it's easy to explain the behavior, I think the decision to dis- > allow mutable items as keys is a bit arbitrary. Furthermore, it's not really true. class Blurf (object): def __init__(self, intval): self.seti(intval) def seti(se

Re: Sets in Python

2007-09-19 Thread prikar20
On Sep 19, 5:25 pm, Mark Dickinson <[EMAIL PROTECTED]> wrote: > On Sep 19, 7:26 pm, Karthik Gurusamy <[EMAIL PROTECTED]> wrote: > > > If I did, a = [10, 20] and I did d[a]= 'foo', then a.append(30). > > If dict complains key error on d[a] now, I won't be surprised. If I do > > d[[10, 20, 30]], I wi

Re: Sets in Python

2007-09-19 Thread Steven D'Aprano
On Wed, 19 Sep 2007 20:58:03 +, Karthik Gurusamy wrote: > While it's easy to explain the behavior, I think the decision to dis- > allow mutable items as keys is a bit arbitrary. There is no need for > dict to recompute hash What??? Of course it does. How else can it look up the key? Because

Re: Sets in Python

2007-09-19 Thread Mark Dickinson
On Sep 19, 7:26 pm, Karthik Gurusamy <[EMAIL PROTECTED]> wrote: > If I did, a = [10, 20] and I did d[a]= 'foo', then a.append(30). > If dict complains key error on d[a] now, I won't be surprised. If I do > d[[10, 20, 30]], I will be surprised if it doesn't find the item. Of > course, in today's beh

Re: Sets in Python

2007-09-19 Thread Karthik Gurusamy
On Sep 19, 3:06 pm, Paddy <[EMAIL PROTECTED]> wrote: > On Sep 19, 9:58 pm, Karthik Gurusamy <[EMAIL PROTECTED]> wrote: > > > Since we know hashing is used, all that is needed is, a well-defined > > way to construct a hash out of a mutable. "Given a sequence, how to > > get a hash" is the problem. I

Re: Sets in Python

2007-09-19 Thread Paddy
On Sep 19, 9:58 pm, Karthik Gurusamy <[EMAIL PROTECTED]> wrote: > > Since we know hashing is used, all that is needed is, a well-defined > way to construct a hash out of a mutable. "Given a sequence, how to > get a hash" is the problem. If later the given sequence is different, > that's not the dic

Re: Sets in Python

2007-09-19 Thread Karthik Gurusamy
On Sep 19, 6:16 am, Sion Arrowsmith <[EMAIL PROTECTED]> wrote: > sapsi <[EMAIL PROTECTED]> wrote: > > Why can't lists be hashed? > > Several people have answered "because they're mutable" without > explaining why mutability precludes hashing. So: > > Consider a dict (dicts have been in Python a *l

Re: Sets in Python

2007-09-19 Thread Sion Arrowsmith
sapsi <[EMAIL PROTECTED]> wrote: > Why can't lists be hashed? Several people have answered "because they're mutable" without explaining why mutability precludes hashing. So: Consider a dict (dicts have been in Python a *lot* longer than sets, and have the same restriction) which allowed lists as

Re: Sets in Python

2007-09-19 Thread Francesco Guerrieri
On 9/19/07, Paddy <[EMAIL PROTECTED]> wrote: > frozenset over turning the embedded list into a tuple? > The tuple would preserve order in the item (1,2) > a = set([1,2,3, (1,2)]) The OP was probably thinking in mathematical terms as in "the set of all the possible subsets of the set composed b

Re: Sets in Python

2007-09-19 Thread Paddy
On Sep 19, 1:59 am, Raymond Hettinger <[EMAIL PROTECTED]> wrote: > On Sep 18, 5:39 pm, sapsi <[EMAIL PROTECTED]> wrote: > > > I recently tried using the set function in Python and was surprised to > > find that > > > a=[ 1, 2,3, [1,2] ] > > > doesn't work with 'set', throwing TyperError (unhashable

Re: Sets in Python

2007-09-18 Thread Dustan
On Sep 18, 7:39 pm, sapsi <[EMAIL PROTECTED]> wrote: > Hello, > I recently tried using the set function in Python and was surprised to > find that > > a=[ 1, 2,3, [1,2] ] > > doesn't work with 'set', throwing TyperError (unhashable exception). I > found out that this is because lists can't be hashe

Re: Sets in Python

2007-09-18 Thread Asun Friere
On Sep 19, 10:39 am, sapsi <[EMAIL PROTECTED]> wrote: > My question is, > 1) Why can't lists be hashed? They are mutable. -- http://mail.python.org/mailman/listinfo/python-list

Re: Sets in Python

2007-09-18 Thread Raymond Hettinger
On Sep 18, 5:39 pm, sapsi <[EMAIL PROTECTED]> wrote: > I recently tried using the set function in Python and was surprised to > find that > > a=[ 1, 2,3, [1,2] ] > > doesn't work with 'set', throwing TyperError (unhashable exception). I > found out that this is because lists can't be hashed. > So,t

Re: Sets in Python

2007-09-18 Thread Evil Bert
sapsi wrote: > 2) This is not related, but is there i neat way (without pop and list > comprehension) to convert a set into a list? I say neat because i'm > guessing using list comprehension might turn out be slow and there > might be other methods which are faster. a = set([1, 2, 3, 4]) b = list(

Sets in Python

2007-09-18 Thread sapsi
Hello, I recently tried using the set function in Python and was surprised to find that a=[ 1, 2,3, [1,2] ] doesn't work with 'set', throwing TyperError (unhashable exception). I found out that this is because lists can't be hashed. So,this implies 'a' cannot be a set in python which i think is