Re: Ordering python sets

2008-11-06 Thread Mr . SpOOn
On Wed, Nov 5, 2008 at 10:03 PM, Arnaud Delobelle [EMAIL PROTECTED] wrote: Only hashable objects can go in a set. By default a class you define is not hashable (unless it descends from a hashable class). To remedy this you can define a __hash__ method in your class. IIRC the only

Re: Ordering python sets

2008-11-05 Thread Mr . SpOOn
The discussion's gone a bit off topic so I don't know if it is a good idea to continue here. I'll try. My first question was about a way to order a python set. Someone suggested to try this module: http://code.activestate.com/recipes/528878/ It seemed pretty good, but I've tried it just today

Re: Ordering python sets

2008-11-05 Thread Arnaud Delobelle
Mr.SpOOn [EMAIL PROTECTED] writes: The discussion's gone a bit off topic so I don't know if it is a good idea to continue here. I'll try. My first question was about a way to order a python set. Someone suggested to try this module: http://code.activestate.com/recipes/528878/ It seemed

Re: Ordering python sets

2008-11-04 Thread Lie Ryan
On Sun, 02 Nov 2008 02:08:37 +, Steven D'Aprano wrote: On Sat, 01 Nov 2008 18:58:59 +, Tim Rowe wrote: 2008/10/27 [EMAIL PROTECTED]: Lie Ryan: Oh no, the two dict implementation would work _exactly_ the same from the outside, they are transparently interchangeable. Only the

Re: Ordering python sets

2008-11-04 Thread MRAB
On Nov 4, 8:40 pm, Lie Ryan [EMAIL PROTECTED] wrote: [snip] On linguistic note: As a non-native speaker of English, I've never relied on the correct usage of Yes and No and would instead rely on the following text. In some languages, situations where English-people usually use Yes is answered

Re: Ordering python sets

2008-11-01 Thread Lie Ryan
On Mon, 27 Oct 2008 13:18:43 -0700, bearophileHUGS wrote: So I don't accept so much different data structures to have the same name You need to adjust the current mindset slightly (but in an important way to understand the why behind this idea). The current notion is: list and dict is a

Re: Ordering python sets

2008-11-01 Thread bearophileHUGS
Lie Ryan: Although you said you disagree with the general idea, you actually take the idea two steps further, I take that as an implicit agreement to several parts of the idea. Think about a bridge: building half bridge may be bad/useless, while building it whole may lead to something useful

Re: Ordering python sets

2008-11-01 Thread Tim Rowe
2008/10/27 [EMAIL PROTECTED]: Lie Ryan: Oh no, the two dict implementation would work _exactly_ the same from the outside, they are transparently interchangeable. Only the performance characteristic differs because of the different implementation. I don't agree with the general idea. If the

Re: Ordering python sets

2008-11-01 Thread Terry Reedy
Lie Ryan wrote: You need to adjust the current mindset slightly (but in an important way to understand the why behind this idea). The current notion is: list and dict is a data structure. With this idea, list and dict is an abstract type, not a data structure. array, linked list, binary tree,

Re: Ordering python sets

2008-11-01 Thread Steven D'Aprano
On Sat, 01 Nov 2008 18:58:59 +, Tim Rowe wrote: 2008/10/27 [EMAIL PROTECTED]: Lie Ryan: Oh no, the two dict implementation would work _exactly_ the same from the outside, they are transparently interchangeable. Only the performance characteristic differs because of the different

Re: Ordering python sets

2008-10-28 Thread Steven D'Aprano
On Mon, 27 Oct 2008 17:03:58 -0700, Glenn Linderman wrote: A little harder question is how to create a key that corresponds to ascending string followed by descending string? To do that you can sort the data two times, relying on the stable nature of the Python sort. Ick. Costs

Re: Ordering python sets

2008-10-27 Thread bearophileHUGS
Glenn Linderman: how does one create a key that corresponds to ascending integer followed by descending character string? (Others may have already answered you because Google groups is very slow.) seq = [(10, abb), (5, zul), (5, hal), (2, of)] sorted(seq, key=lambda (n,s): (-n, s),

Re: Ordering python sets

2008-10-27 Thread Peter Otten
[EMAIL PROTECTED] wrote: Glenn Linderman: how does one create a key that corresponds to ascending integer followed by descending character string? (Others may have already answered you because Google groups is very slow.) seq = [(10, abb), (5, zul), (5, hal), (2, of)] sorted(seq,

Re: Ordering python sets

2008-10-27 Thread bearophileHUGS
Lie Ryan: Oh no, the two dict implementation would work _exactly_ the same from the outside, they are transparently interchangeable. Only the performance characteristic differs because of the different implementation. I don't agree with the general idea. If the operations done by your data

Re: Ordering python sets

2008-10-27 Thread Carl Banks
On Oct 25, 4:58 am, Lie Ryan [EMAIL PROTECTED] wrote: On Wed, 22 Oct 2008 10:43:35 -0700, bearophileHUGS wrote: Mr.SpOOn: Is there another convenient structure or shall I use lists and define the operations I need? musings As Python becomes accepted for more and more serious projects

Re: Ordering python sets

2008-10-27 Thread greg
On approximately 10/27/2008 10:27 AM, came the following characters from the keyboard of Peter Otten: Here's a class that can negate arbitrary values ... def __init__(self, value): ... self.value = value ... def __cmp__(self, other): ... return -cmp(self.value,

Re: Ordering python sets

2008-10-26 Thread Marc 'BlackJack' Rintsch
On Sat, 25 Oct 2008 21:53:10 +, Lie Ryan wrote: Oh no, the two dict implementation would work _exactly_ the same from the outside, they are transparently interchangeable. Only the performance characteristic differs because of the different implementation. They are not 100%

Re: Ordering python sets

2008-10-26 Thread Lie Ryan
On Sun, 26 Oct 2008 00:53:18 +, Steven D'Aprano wrote: [...] And how do you find an arbitrary object's creation point without searching the project's source code? How is it better using the current way? Asking the .implementation field isn't much harder than asking the type (), and is much

Re: Ordering python sets

2008-10-26 Thread Lie Ryan
On Sat, 25 Oct 2008 21:50:36 -0400, Terry Reedy wrote: Lie Ryan wrote: On Sat, 25 Oct 2008 18:20:46 -0400, Terry Reedy wrote: Then why do you object to current mylist = linkedlist(data) and request the harder to write and implement mylist = list(data, implementation =

Re: Ordering python sets

2008-10-25 Thread Lie Ryan
On Wed, 22 Oct 2008 10:43:35 -0700, bearophileHUGS wrote: Mr.SpOOn: Is there another convenient structure or shall I use lists and define the operations I need? musings As Python becomes accepted for more and more serious projects some more data structures can eventually be added to the

Re: Ordering python sets

2008-10-25 Thread Steven D'Aprano
On Sat, 25 Oct 2008 08:58:18 +, Lie Ryan wrote: anotherrandommusing Since python is dynamic language, I think it should be possible to do something like this: a = list([1, 2, 3, 4, 5], implementation = 'linkedlist') b = dict({'a': 'A'}, implementation = 'binarytree') c = dict({'a':

Re: Ordering python sets

2008-10-25 Thread Lie Ryan
On Sat, 25 Oct 2008 09:21:05 +, Steven D'Aprano wrote: On Sat, 25 Oct 2008 08:58:18 +, Lie Ryan wrote: anotherrandommusing Since python is dynamic language, I think it should be possible to do something like this: a = list([1, 2, 3, 4, 5], implementation = 'linkedlist') b =

Re: Ordering python sets

2008-10-25 Thread Terry Reedy
Lie Ryan wrote: anotherrandommusing Since python is dynamic language, I think it should be possible to do something like this: a = list([1, 2, 3, 4, 5], implementation = 'linkedlist') For this to work, the abstract list would have to know about all implementations of the abstraction.

Re: Ordering python sets

2008-10-25 Thread Lie Ryan
On Sat, 25 Oct 2008 18:20:46 -0400, Terry Reedy wrote: Lie Ryan wrote: anotherrandommusing Since python is dynamic language, I think it should be possible to do something like this: a = list([1, 2, 3, 4, 5], implementation = 'linkedlist') For this to work, the abstract list would

Re: Ordering python sets

2008-10-25 Thread Lie Ryan
On Sat, 25 Oct 2008 18:20:46 -0400, Terry Reedy wrote: Lie Ryan wrote: anotherrandommusing Since python is dynamic language, I think it should be possible to do something like this: a = list([1, 2, 3, 4, 5], implementation = 'linkedlist') For this to work, the abstract list would

Re: Ordering python sets

2008-10-25 Thread Steven D'Aprano
On Sat, 25 Oct 2008 21:53:10 +, Lie Ryan wrote: On Sat, 25 Oct 2008 09:21:05 +, Steven D'Aprano wrote: On Sat, 25 Oct 2008 08:58:18 +, Lie Ryan wrote: anotherrandommusing Since python is dynamic language, I think it should be possible to do something like this: a =

Re: Ordering python sets

2008-10-25 Thread Terry Reedy
Lie Ryan wrote: On Sat, 25 Oct 2008 18:20:46 -0400, Terry Reedy wrote: a = list([1, 2, 3, 4, 5], implementation = 'linkedlist') For this to work, the abstract list would have to know about all implementations of the abstraction. /the exact syntax isn't really important/ /abstract type and

Re: Ordering python sets

2008-10-23 Thread Duncan Booth
Peter Otten [EMAIL PROTECTED] wrote: I guess I have to move the goal posts to beat you: set([-1,-2]), set([-2,-1]) (set([-2, -1]), set([-1, -2])) For that one the number of slots doesn't matter because hash(-1), hash(-2) (-2, -2) Neat. last = [] for i in range(0,1,5):

Ordering python sets

2008-10-22 Thread Mr . SpOOn
Hi, I need a structure to represent a set of integers. I also need to perform on this set some basic set operations, such as adding or removing elements, joining with other sets and checking for the presence of specific elements. I think that using Python sets would be the best choice, but I also

Re: Ordering python sets

2008-10-22 Thread Tim Chase
I think that using Python sets would be the best choice, but I also need integers to be ordered inside the set and I've just found out that, instead, Python sets are unordered collections. Sets (both in Python, and their mathematical definition) are unordered. However, some simple testing in

Re: Ordering python sets

2008-10-22 Thread Peter Otten
Tim Chase wrote: Though for each test, in 2.3, 2.4, and 2.5 that I've got installed on my local machine, they each printed s in-order, and the iteration occurred in-order as well, even without the added sorted(list(s)) code. You need more tests then ;) list(set([1,1000])) [1000, 1] By the

Re: Ordering python sets

2008-10-22 Thread Mr . SpOOn
On Wed, Oct 22, 2008 at 3:37 PM, Peter Otten [EMAIL PROTECTED] wrote: Tim Chase wrote: Though for each test, in 2.3, 2.4, and 2.5 that I've got installed on my local machine, they each printed s in-order, and the iteration occurred in-order as well, even without the added sorted(list(s))

Re: Ordering python sets

2008-10-22 Thread Roy Smith
In article [EMAIL PROTECTED], Mr.SpOOn [EMAIL PROTECTED] wrote: It seems to me that it orders elements when you add using the add() method, but if you create a set starting from a list, it may result unordered. Arrrggghhh! None of these behaviors are guaranteed. The docs say, A set object

Re: Ordering python sets

2008-10-22 Thread Mr . SpOOn
On Wed, Oct 22, 2008 at 4:30 PM, Roy Smith [EMAIL PROTECTED] wrote: In article [EMAIL PROTECTED], Mr.SpOOn [EMAIL PROTECTED] wrote: It seems to me that it orders elements when you add using the add() method, but if you create a set starting from a list, it may result unordered.

Re: Ordering python sets

2008-10-22 Thread Peter Pearson
On Wed, 22 Oct 2008 15:37:03 +0200, Peter Otten [EMAIL PROTECTED] wrote: Tim Chase wrote: Though for each test, in 2.3, 2.4, and 2.5 that I've got installed on my local machine, they each printed s in-order, and the iteration occurred in-order as well, even without the added sorted(list(s))

Re: Ordering python sets

2008-10-22 Thread Peter Otten
Peter Pearson wrote: On Wed, 22 Oct 2008 15:37:03 +0200, Peter Otten [EMAIL PROTECTED] wrote: Tim Chase wrote: Though for each test, in 2.3, 2.4, and 2.5 that I've got installed on my local machine, they each printed s in-order, and the iteration occurred in-order as well, even without the

Re: Ordering python sets

2008-10-22 Thread bearophileHUGS
Mr.SpOOn: Is there another convenient structure or shall I use lists and define the operations I need? musings As Python becomes accepted for more and more serious projects some more data structures can eventually be added to the collections module: - SortedSet, SortedDict: can be based on

Re: Ordering python sets

2008-10-22 Thread Duncan Booth
Peter Otten [EMAIL PROTECTED] wrote: Here's another one: set([1,9]) set([1, 9]) set([9,1]) set([9, 1]) This time I did indeed search systematically... You missed one with smaller values: set([8,0]) set([8, 0]) set([0,8]) set([0, 8]) You can work some of it out quite easily instead

Re: Ordering python sets

2008-10-22 Thread Peter Otten
Duncan Booth wrote: Peter Otten [EMAIL PROTECTED] wrote: Here's another one: set([1,9]) set([1, 9]) set([9,1]) set([9, 1]) This time I did indeed search systematically... You missed one with smaller values: set([8,0]) set([8, 0]) set([0,8]) set([0, 8]) I searched the minimal