Re: [Python-Dev] slightly inconsistent set/list pop behaviour
> foo = set([1, 65537]) > foo.pop() >> 1 > foo = set([65537, 1]) > foo.pop() >> 65537 > > You wrote a program to find the two smallest ints that would have a > hash collision in the CPython set implementation? I'm impressed. And > by impressed I mean frightened. Well, Mark is the guy who deals with floating point numbers for fun. *That* should frighten you :-) Martin ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] slightly inconsistent set/list pop behaviour
2009/4/8 Steve Holden : > Paul Moore wrote: >> 2009/4/8 Duncan Booth : >>> Andrea Griffini wrote: >>> On Wed, Apr 8, 2009 at 12:57 PM, Jack diederich wrote: > You wrote a program to find the two smallest ints that would have a > hash collision in the CPython set implementation? I'm impressed. > And by impressed I mean frightened. ? print set([0,8]).pop(), set([8,0]).pop() >>> If 'smallest ints' means the sum of the absolute values then these are >>> slightly smaller: >>> >> print set([-1,6]).pop(), set([6,-1]).pop() >>> 6 -1 >> >> Can't resist: >> > print set([-2,-1]).pop(), set([-1,-2]).pop() >> -1 -2 >> a = 0.001 b = 0.002 print set([a, b]).pop(), set([b, a]).pop() > 0.002 0.001 Cheat! We were using integers... :-) Paul. ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] slightly inconsistent set/list pop behaviour
Paul Moore wrote: > 2009/4/8 Duncan Booth : >> Andrea Griffini wrote: >> >>> On Wed, Apr 8, 2009 at 12:57 PM, Jack diederich >>> wrote: You wrote a program to find the two smallest ints that would have a hash collision in the CPython set implementation? I'm impressed. And by impressed I mean frightened. >>> ? >>> >>> print set([0,8]).pop(), set([8,0]).pop() >> If 'smallest ints' means the sum of the absolute values then these are >> slightly smaller: >> > print set([-1,6]).pop(), set([6,-1]).pop() >> 6 -1 > > Can't resist: > print set([-2,-1]).pop(), set([-1,-2]).pop() > -1 -2 > >>> a = 0.001 >>> b = 0.002 >>> print set([a, b]).pop(), set([b, a]).pop() 0.002 0.001 Let's stop here ... regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ Watch PyCon on video now! http://pycon.blip.tv/ ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] slightly inconsistent set/list pop behaviour
2009/4/8 Duncan Booth : > Andrea Griffini wrote: > >> On Wed, Apr 8, 2009 at 12:57 PM, Jack diederich >> wrote: >>> You wrote a program to find the two smallest ints that would have a >>> hash collision in the CPython set implementation? I'm impressed. >>> And by impressed I mean frightened. >> >> ? >> >> print set([0,8]).pop(), set([8,0]).pop() > > If 'smallest ints' means the sum of the absolute values then these are > slightly smaller: > print set([-1,6]).pop(), set([6,-1]).pop() > 6 -1 Can't resist: >>> print set([-2,-1]).pop(), set([-1,-2]).pop() -1 -2 Paul. ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] slightly inconsistent set/list pop behaviour
Andrea Griffini wrote: > On Wed, Apr 8, 2009 at 12:57 PM, Jack diederich > wrote: >> You wrote a program to find the two smallest ints that would have a >> hash collision in the CPython set implementation? I'm impressed. >> And by impressed I mean frightened. > > ? > > print set([0,8]).pop(), set([8,0]).pop() If 'smallest ints' means the sum of the absolute values then these are slightly smaller: >>> print set([-1,6]).pop(), set([6,-1]).pop() 6 -1 ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] slightly inconsistent set/list pop behaviour
On Wed, Apr 8, 2009 at 12:57 PM, Jack diederich wrote: > You wrote a program to find the two smallest ints that would have a > hash collision in the CPython set implementation? I'm impressed. And > by impressed I mean frightened. ? print set([0,8]).pop(), set([8,0]).pop() Andrea ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] slightly inconsistent set/list pop behaviour
Jack diederich wrote: > On Wed, Apr 8, 2009 at 2:44 AM, Mark Dickinson wrote: >> On Wed, Apr 8, 2009 at 7:13 AM, John Barham wrote: >>> If you play around a bit it becomes clear that what set.pop() returns >>> is independent of the insertion order: >> It might look like that, but I don't think this is >> true in general (at least, with the current implementation): >> > foo = set([1, 65537]) > foo.pop() >> 1 > foo = set([65537, 1]) > foo.pop() >> 65537 > > You wrote a program to find the two smallest ints that would have a > hash collision in the CPython set implementation? I'm impressed. And > by impressed I mean frightened. > Given the two numbers in question (1, 2**16+1) I suspect this is the result of analysis rather than algorithm. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ Watch PyCon on video now! http://pycon.blip.tv/ ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] slightly inconsistent set/list pop behaviour
On Wed, Apr 8, 2009 at 2:44 AM, Mark Dickinson wrote: > On Wed, Apr 8, 2009 at 7:13 AM, John Barham wrote: >> If you play around a bit it becomes clear that what set.pop() returns >> is independent of the insertion order: > > It might look like that, but I don't think this is > true in general (at least, with the current implementation): > foo = set([1, 65537]) foo.pop() > 1 foo = set([65537, 1]) foo.pop() > 65537 You wrote a program to find the two smallest ints that would have a hash collision in the CPython set implementation? I'm impressed. And by impressed I mean frightened. -Jack ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] slightly inconsistent set/list pop behaviour
Mark Dickinson gmail.com> writes: > > On Wed, Apr 8, 2009 at 7:13 AM, John Barham gmail.com> wrote: > > If you play around a bit it becomes clear that what set.pop() returns > > is independent of the insertion order: > > It might look like that, but I don't think this is > true in general (at least, with the current implementation): Not to mention that other implementations (Jython, etc.) will probably exhibit yet different behaviour, and the CPython hash functions are not engraved in stone either. If you want to write portable code, you can't rely on *any* reproduceable ordering for random set member access. Regards Antoine. ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] slightly inconsistent set/list pop behaviour
On Wed, Apr 8, 2009 at 7:13 AM, John Barham wrote: > If you play around a bit it becomes clear that what set.pop() returns > is independent of the insertion order: It might look like that, but I don't think this is true in general (at least, with the current implementation): >>> foo = set([1, 65537]) >>> foo.pop() 1 >>> foo = set([65537, 1]) >>> foo.pop() 65537 Mark ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] slightly inconsistent set/list pop behaviour
Tennessee Leeuwenburg wrote: > Now, I know that sets aren't ordered, but... > > foo = set([1,2,3,4,5]) > bar = [1,2,3,4,5] > > foo.pop() will reliably return 1 > while bar.pop() will return 5 > > discuss :) As designed. If you play around a bit it becomes clear that what set.pop() returns is independent of the insertion order: PythonWin 2.5.2 (r252:60911, Mar 27 2008, 17:57:18) [MSC v.1310 32 bit (Intel)] on win32. >>> foo = set([5,4,3,2,1]) # Order reversed from above >>> foo.pop() 1 >>> foo = set([-1,0,1,2,3,4,5]) >>> foo.pop() 0 >>> foo = set([-1,1,2,3,4,5]) >>> foo.pop() 1 As the documentation says (http://docs.python.org/library/stdtypes.html#set.pop) set.pop() is free to return an arbitrary element. list.pop() however always returns the last element of the list, unless of course you specify some other index: http://docs.python.org/library/stdtypes.html#mutable-sequence-types, point 6. John ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] slightly inconsistent set/list pop behaviour
[Tennessee Leeuwenburg ] Now, I know that sets aren't ordered, but... foo = set([1,2,3,4,5]) bar = [1,2,3,4,5] foo.pop() will reliably return 1 while bar.pop() will return 5 discuss :) If that's what you need: http://code.activestate.com/recipes/576694/ Raymond ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
[Python-Dev] slightly inconsistent set/list pop behaviour
Now, I know that sets aren't ordered, but... foo = set([1,2,3,4,5]) bar = [1,2,3,4,5] foo.pop() will reliably return 1 while bar.pop() will return 5 discuss :) Cheers, -T ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com