Re: Sorted dictionary

2010-01-21 Thread Arnaud Delobelle
"Jan Kaliszewski" writes: > Dnia 21-01-2010 o 09:27:52 Raymond Hettinger napisał(a): > >> On Jan 20, 5:02 pm, "Jan Kaliszewski" wrote: > >>> http://code.activestate.com/recipes/576998/ > >> Using an underlying list to track sorted items >> means that insertion and deletion take O(n) time. >> Th

Re: Sorted dictionary

2010-01-21 Thread Daniel Stutzbach
On Thu, Jan 21, 2010 at 12:45 PM, Jan Kaliszewski wrote: > Please note that I used funcions from bisect, that use binary search. > > Doesn't it take O(log n) time? > It takes O(log n) time to find the point to insert, but O(n) time to perform the actual insertion. -- Daniel Stutzbach, Ph.D. Pre

Re: Sorted dictionary

2010-01-21 Thread Jan Kaliszewski
Dnia 21-01-2010 o 09:27:52 Raymond Hettinger napisał(a): On Jan 20, 5:02 pm, "Jan Kaliszewski" wrote: http://code.activestate.com/recipes/576998/ Using an underlying list to track sorted items means that insertion and deletion take O(n) time. That could be reduced to O(log n) time by usi

Re: Sorted dictionary

2010-01-21 Thread Jan Kaliszewski
Dnia 21-01-2010 o 08:49:22 Chris Rebert napisał(a): On Wed, Jan 20, 2010 at 5:50 PM, Steven D'Aprano wrote: On Thu, 21 Jan 2010 02:02:02 +0100, Jan Kaliszewski wrote: http://code.activestate.com/recipes/576998/ What's the advantage of that over sorting the keys as needed? E.g. for ke

Re: Sorted dictionary

2010-01-21 Thread Jan Kaliszewski
21-01-2010, 07:21:22 Dennis Lee Bieber wrote: How does this differ from all the other "ordered dictionary" schemes out there (and maybe even included in 2.7? I'm still on 2.5) It's completely different, please read first paragraph of page I linked (http://code.activestate.com/recipe

Re: Sorted dictionary

2010-01-21 Thread Daniel Stutzbach
On Thu, Jan 21, 2010 at 2:27 AM, Raymond Hettinger wrote: > Using an underlying list to track sorted items > means that insertion and deletion take O(n) time. > That could be reduced to O(log n) time by using > a blist or skiplist as the underlying structure > for maintaining a sort. > Indeed.

Re: Sorted dictionary

2010-01-21 Thread Bearophile
Raymond Hettinger: > Using an underlying list to track sorted items > means that insertion and deletion take O(n) time. > That could be reduced to O(log n) time by using > a blist or skiplist as the underlying structure > for maintaining a sort. In the collections module it can be useful to have o

Re: Sorted dictionary

2010-01-21 Thread Raymond Hettinger
On Jan 20, 5:02 pm, "Jan Kaliszewski" wrote: > Hello, > > Inspired by some my needs as well as some discussions in the net, I've   > implemented a sorted dictionary (i.e. a dict that returns keys, values and   > items always sorted by keys): > > http://code.activestate.com/recipes/576998/ > > Mayb

Re: Sorted dictionary

2010-01-20 Thread Chris Rebert
On Wed, Jan 20, 2010 at 5:50 PM, Steven D'Aprano wrote: > On Thu, 21 Jan 2010 02:02:02 +0100, Jan Kaliszewski wrote: > >> Hello, >> >> Inspired by some my needs as well as some discussions in the net, I've >> implemented a sorted dictionary (i.e. a dict that returns keys, values >> and items alway

Re: Sorted dictionary

2010-01-20 Thread Steven D'Aprano
On Wed, 20 Jan 2010 22:21:22 -0800, Dennis Lee Bieber wrote: > On Thu, 21 Jan 2010 02:02:02 +0100, "Jan Kaliszewski" > declaimed the following in > gmane.comp.python.general: > >> Hello, >> >> Inspired by some my needs as well as some discussions in the net, I've >> implemented a sorted diction

Re: Sorted dictionary

2010-01-20 Thread Steven D'Aprano
On Thu, 21 Jan 2010 02:02:02 +0100, Jan Kaliszewski wrote: > Hello, > > Inspired by some my needs as well as some discussions in the net, I've > implemented a sorted dictionary (i.e. a dict that returns keys, values > and items always sorted by keys): > > http://code.activestate.com/recipes/5769

Re: sorted() erraticly fails to sort string numbers

2009-04-30 Thread Paddy O'Loughlin
2009/4/30 Lie Ryan > container[:] = sorted(container, key=getkey) >> >> is equivalent to: >> >> container.sort(key=getkey) >> >> > Equivalent, and in fact better since the sorting is done in-place instead > of creating a new list, then overwriting the old one. Not when, as pointed out

Re: sorted() erraticly fails to sort string numbers

2009-04-30 Thread Lie Ryan
John Posner wrote: uuid wrote: I am at the same time impressed with the concise answer and disheartened by my inability to see this myself. My heartfelt thanks! Don't be disheartened! Many people -- myself included, absolutely! -- occasionally let a blind spot show in their messages to this li

Re: sorted() erraticly fails to sort string numbers

2009-04-28 Thread uuid
On 2009-04-28 16:18:43 +0200, John Posner said: Don't be disheartened! Many people -- myself included, absolutely! -- occasionally let a blind spot show in their messages to this list. Thanks for the encouragement :) BTW: container[:] = sorted(container, key=getkey) ... is equivalent

Re: Re: sorted() erraticly fails to sort string numbers

2009-04-28 Thread John Posner
uuid wrote: I am at the same time impressed with the concise answer and disheartened by my inability to see this myself. My heartfelt thanks! Don't be disheartened! Many people -- myself included, absolutely! -- occasionally let a blind spot show in their messages to this list. BTW: contai

Re: sorted() erraticly fails to sort string numbers

2009-04-28 Thread uuid
I am at the same time impressed with the concise answer and disheartened by my inability to see this myself. My heartfelt thanks! On 2009-04-28 10:06:24 +0200, Andre Engels said: When sorting strings, including strings that represent numbers, sorting is done alphabetically. In this alphabeti

Re: sorted() erraticly fails to sort string numbers

2009-04-28 Thread Andre Engels
On Tue, Apr 28, 2009 at 9:47 AM, uuid wrote: > I would be very interested in a logical explanation why this happens on > python 2.5.1: > > In order to sort an etree by the .text value of one child, I adapted this > snippet from effbot.org: > >> import xml.etree.ElementTree as ET >> >> tree = ET.pa

Re: Sorted Returns List and Reversed Returns Iterator

2008-08-22 Thread ++imanshu
On Aug 22, 12:36 pm, Terry Reedy <[EMAIL PROTECTED]> wrote: > Peter Otten wrote: > > ++imanshu wrote: > >> I agree. Iterator is more flexible. > > I disagree.  Neither is more flexible.  You can iter the list returned > by sorted and list the iter returned by reversed.  Both do the minimum > work n

Re: Sorted Returns List and Reversed Returns Iterator

2008-08-22 Thread Terry Reedy
Peter Otten wrote: ++imanshu wrote: I agree. Iterator is more flexible. I disagree. Neither is more flexible. You can iter the list returned by sorted and list the iter returned by reversed. Both do the minimum work necessary. See below. > Together and both might have returned the

Re: Sorted Returns List and Reversed Returns Iterator

2008-08-22 Thread Fredrik Lundh
John Machin wrote: reversed came later; returning an iterator rather than a list provides more flexibility. As in flexibility for the implementer, the day someone invents a sort algorithm that doesn't have to look at all source items before it starts producing output? Because I fail to see

Re: Sorted Returns List and Reversed Returns Iterator

2008-08-21 Thread Peter Otten
++imanshu wrote: > On Aug 22, 8:40 am, John Machin <[EMAIL PROTECTED]> wrote: >> On Aug 22, 1:35 pm, John Machin <[EMAIL PROTECTED]> wrote: >> >> >> >> > On Aug 22, 12:12 pm, "++imanshu" <[EMAIL PROTECTED]> wrote: >> >> > > Hi, >> >> > > Is there a reason why two similarly named functions Sorted a

Re: Sorted Returns List and Reversed Returns Iterator

2008-08-21 Thread ++imanshu
On Aug 22, 8:40 am, John Machin <[EMAIL PROTECTED]> wrote: > On Aug 22, 1:35 pm, John Machin <[EMAIL PROTECTED]> wrote: > > > > > On Aug 22, 12:12 pm, "++imanshu" <[EMAIL PROTECTED]> wrote: > > > > Hi, > > > >      Is there a reason why two similarly named functions Sorted and > > > Reversed return

Re: Sorted Returns List and Reversed Returns Iterator

2008-08-21 Thread John Machin
On Aug 22, 1:35 pm, John Machin <[EMAIL PROTECTED]> wrote: > On Aug 22, 12:12 pm, "++imanshu" <[EMAIL PROTECTED]> wrote: > > > Hi, > > >      Is there a reason why two similarly named functions Sorted and > > Reversed return different types of data or is it an accident. > > You seem to have an inte

Re: Sorted Returns List and Reversed Returns Iterator

2008-08-21 Thread John Machin
On Aug 22, 12:12 pm, "++imanshu" <[EMAIL PROTECTED]> wrote: > Hi, > >      Is there a reason why two similarly named functions Sorted and > Reversed return different types of data or is it an accident. You seem to have an interesting notion of "similarly named". name0[-2:] == name1[-2:], perhaps?

Re: sorted or .sort() ?

2008-06-16 Thread Nick Craig-Wood
Ben Finney <[EMAIL PROTECTED]> wrote: > Peter Bengtsson <[EMAIL PROTECTED]> writes: > > > My poor understanding is that the difference between `sorted(somelist, > > key=lambda x:...)` and `somelist.sort(lambda x,y...)` is that one > > returns a new list and the other sorts in-place. > > Yes. >

Re: sorted or .sort() ?

2008-06-16 Thread Raymond Hettinger
On Jun 16, 5:11 am, Peter Bengtsson <[EMAIL PROTECTED]> wrote: > My poor understanding is that the difference between `sorted(somelist, > key=lambda x:...)` and `somelist.sort(lambda x,y...)` is that one > returns a new list and the other sorts in-place. > > Does that mean that .sort() is more effi

Re: sorted or .sort() ?

2008-06-16 Thread Hrvoje Niksic
Peter Bengtsson <[EMAIL PROTECTED]> writes: > Does that mean that .sort() is more efficient and should be favored > when you can (i.e. when you don't mind changing the listish object)? Yes. Note that it's not "the listish object", the "sort" method is implemented on actual lists, not on any sequ

Re: sorted or .sort() ?

2008-06-16 Thread Ben Finney
Peter Bengtsson <[EMAIL PROTECTED]> writes: > My poor understanding is that the difference between `sorted(somelist, > key=lambda x:...)` and `somelist.sort(lambda x,y...)` is that one > returns a new list and the other sorts in-place. Yes. > Does that mean that .sort() is more efficient and sho

Re: Sorted list - how to change it

2006-11-09 Thread Nick Craig-Wood
Tim Chase <[EMAIL PROTECTED]> wrote: > Just a caveat from past experience...while the OP was talking > about lists, for future reference random.shuffle() chokes on > strings (and possibly tuples). It requires the ability to edit > the target/parameter in place...a functionality that strings

Re: Sorted list - how to change it

2006-11-09 Thread Christophe
Lad a écrit : > I have a sorted list for example [1,2,3,4,5] and I would like to change > it in a random way > e.g [2,5,3,1,4] or [3,4,1,5,2] or in any other way except being > ordered. > What is the best/easiest > way how to do it? > > Thank you for help > L. Not accepting that the shuffle outp

Re: Sorted list - how to change it

2006-11-09 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Marc 'BlackJack' Rintsch wrote: > lst = [1, 2, 3, 4, 5] > tmp = list(lst) > while lst == tmp: > random.shuffle(lst) Argh, that fails if the list is empty or contains just one item. lst = [1, 2, 3, 4, 5] if len(lst) > 1: tmp = list(lst) while lst == tmp:

Re: Sorted list - how to change it

2006-11-09 Thread Tim Chase
>> I have a sorted list for example [1,2,3,4,5] and I would like to change >> it in a random way >> e.g [2,5,3,1,4] or [3,4,1,5,2] or in any other way except being >> ordered. >> What is the best/easiest >> way how to do it? > > use random.shuffel: > import random x = [1,2,3,4,5]

Re: Sorted list - how to change it

2006-11-09 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Lad wrote: > I have a sorted list for example [1,2,3,4,5] and I would like to change > it in a random way > e.g [2,5,3,1,4] or [3,4,1,5,2] or in any other way except being > ordered. lst = [1, 2, 3, 4, 5] tmp = list(lst) while lst == tmp: random.shuffle(lst) If you *

Re: Sorted list - how to change it

2006-11-09 Thread Lad
Wolfram Kraus wrote: > On 09.11.2006 10:52, Lad wrote: > > I have a sorted list for example [1,2,3,4,5] and I would like to change > > it in a random way > > e.g [2,5,3,1,4] or [3,4,1,5,2] or in any other way except being > > ordered. > > What is the best/easiest > > way how to do it? > > > > Th

Re: Sorted list - how to change it

2006-11-09 Thread Wolfram Kraus
On 09.11.2006 10:52, Lad wrote: > I have a sorted list for example [1,2,3,4,5] and I would like to change > it in a random way > e.g [2,5,3,1,4] or [3,4,1,5,2] or in any other way except being > ordered. > What is the best/easiest > way how to do it? > > Thank you for help > L. > use random.shu

Re: Sorted and reversed on huge dict ?

2006-11-06 Thread Sion Arrowsmith
<[EMAIL PROTECTED]> wrote: >so i just have tried, even if i think it will not go to the end => i >was wrong : it is around 1.400.000 entries by dict... > >but maybe if keys of dicts are not duplicated in memory it can be done >(as all dicts will have the same keys, with different (count) values)?

Re: Sorted and reversed on huge dict ?

2006-11-04 Thread vd12005
just to be sure about intern, it is used as : >>> d, f = {}, {} >>> s = "this is a string" >>> d[intern(s)] = 1 >>> f[intern(s)] = 1 so actually the key in d and f are a pointer on an the same intern-ed string? if so it can be interesting, >>> print intern.__doc__ intern(string) -> string ``In

Re: Sorted and reversed on huge dict ?

2006-11-04 Thread vd12005
so it has worked :) and last 12h4:56, 15 dicts with 1133755 keys, i do not know how much ram was used as i was not always monitoring it. thanks for all replies, i'm going to study intern and others suggestions, hope also someone will bring a pythonic way to know memory usage :) best. -- http:/

Re: Sorted and reversed on huge dict ?

2006-11-04 Thread Nick Craig-Wood
Paul Rubin wrote: > > is there a good way to know how much ram is used directly from > > python (or should i rely on 'top' and other unix command? > > I think try resource.getrusage() That should work (under BSD anyway) but unfortunately doesn't under linux :-( >From the man page CONFORMIN

Re: Sorted and reversed on huge dict ?

2006-11-04 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote: > Klaas > i do not know about intern construct, i will have look, but > when googling the description in the documentation is pretty concise, I think: http://effbot.org/pyref/intern -- http://mail.python.org/mailman/listinfo/python-list

Re: Sorted and reversed on huge dict ?

2006-11-03 Thread vd12005
so it still unfinished :) around 1GB for 1033268 words :) (comes from a top unix command) Paul > i was also thinking on doing it like that by pip-ing to 'sort | uniq -c | sort -nr' , but i'm pleased if Python can handle it. (well but maybe Python is slower? will check later...) Klaas > i do not

Re: Sorted and reversed on huge dict ?

2006-11-03 Thread Klaas
[EMAIL PROTECTED] wrote: > thanks for your replies :) > > so i just have tried, even if i think it will not go to the end => i > was wrong : it is around 1.400.000 entries by dict... > > but maybe if keys of dicts are not duplicated in memory it can be done > (as all dicts will have the same keys,

Re: Sorted and reversed on huge dict ?

2006-11-03 Thread Paul Rubin
[EMAIL PROTECTED] writes: > but maybe if keys of dicts are not duplicated in memory it can be done > (as all dicts will have the same keys, with different (count) values)? There will still be a pointer for each key, the strings themselves won't be duplicated. > memory is 4Gb of ram, That soun

Re: Sorted and reversed on huge dict ?

2006-11-03 Thread vd12005
thanks for your replies :) so i just have tried, even if i think it will not go to the end => i was wrong : it is around 1.400.000 entries by dict... but maybe if keys of dicts are not duplicated in memory it can be done (as all dicts will have the same keys, with different (count) values)? memo

Re: Sorted and reversed on huge dict ?

2006-11-03 Thread Fredrik Lundh
Paul Rubin wrote: >> items = d.items() >> items.sort(key=operator.itemgetter(1), reverse=True) >> >> the items list would require a couple of megabytes for 150k dictionary >> entries, or so. the key map needs some memory too, but the rest of >> the sort is done in place. > > I think th

Re: Sorted and reversed on huge dict ?

2006-11-03 Thread Paul Rubin
Fredrik Lundh <[EMAIL PROTECTED]> writes: > items = d.items() > items.sort(key=operator.itemgetter(1), reverse=True) > > the items list would require a couple of megabytes for 150k dictionary > entries, or so. the key map needs some memory too, but the rest of > the sort is done in plac

Re: Sorted and reversed on huge dict ?

2006-11-03 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote: > i would like to sort(ed) and reverse(d) the result of many huge > dictionaries (a single dictionary will contain ~ 15 entries). Keys > are words, values are count (integer). not sure 150k entries qualify as huge, though, unless you're short on memory. > i'm wonder

Re: Sorted and reversed on huge dict ?

2006-11-03 Thread Paul Rubin
[EMAIL PROTECTED] writes: > i would like to sort(ed) and reverse(d) the result of many huge > dictionaries (a single dictionary will contain ~ 15 entries). Keys > are words, values are count (integer). > > i'm wondering if i can have a 10s of these in memory, Depends on how much memory your

Re: sorted

2006-08-18 Thread thomas
Dennis Lee Bieber skrev: > On Fri, 18 Aug 2006 09:31:54 +0200, thomas <[EMAIL PROTECTED]> declaimed > the following in comp.lang.python: > >> I what to used the sorted function, and im getting this error >> > >> what do I needs to import, to use this function ? >> > Uhm... the entire

Re: Sorted List (binary tree) why no built-in/module?

2005-06-04 Thread =?iso-8859-1?Q?Fran=E7ois?= Pinard
[Alex Stapleton] > Unless I've totally missed it, there isn't a binary tree/sorted list > type arrangement in Python. Sometimes it might be preferable over > using a list and calling list.sort() all the time ;) Well, after `some_list.sort()', `some_list' is indeed a sorted list. :-) You can use

Re: Sorted List (binary tree) why no built-in/module?

2005-06-04 Thread Robert Kern
Alex Stapleton wrote: > Unless I've totally missed it, there isn't a binary tree/sorted list > type arrangement in Python. Is there a particular reason for this? > Sometimes it might be preferable over using a list and calling > list.sort() all the time ;) Well, I believe that list.sort() ha

RE: sorted (WAS: lambda)

2005-01-13 Thread Delaney, Timothy C (Timothy)
Terry Reedy wrote: > No, not same difference. A list method would only operate on lists, > as is true of all list methods. Being a function lets it work for > any iterable, as is true of any function of iterable. Big > difference. And consistent. One could argue though that it should > have be

Re: sorted (WAS: lambda)

2005-01-13 Thread Terry Reedy
"Paul Rubin" <"http://phr.cx"@NOSPAM.invalid> wrote in message news:[EMAIL PROTECTED] > Steven Bethard <[EMAIL PROTECTED]> writes: >> Note that sorted is a builtin function, not a method of a list >> object. > > Oh, same difference. I thought it was a method because I'm not using > 2.4 yet. The

Re: sorted (WAS: lambda)

2005-01-13 Thread Paul Rubin
"Fredrik Lundh" <[EMAIL PROTECTED]> writes: > > Oh, same difference. I thought it was a method because I'm not using > > 2.4 yet. The result is the same > > nope. sorted works on any kind of sequence, including forward-only > iterators. sorted(open(filename)) works just fine, for example. Oh

Re: sorted (WAS: lambda)

2005-01-13 Thread Fredrik Lundh
Paul Rubin wrote: >> Note that sorted is a builtin function, not a method of a list >> object. > > Oh, same difference. I thought it was a method because I'm not using > 2.4 yet. The result is the same nope. sorted works on any kind of sequence, including forward-only iterators. sorted(open(f

Re: sorted (WAS: lambda)

2005-01-13 Thread Paul Rubin
Steven Bethard <[EMAIL PROTECTED]> writes: > Note that sorted is a builtin function, not a method of a list > object. Oh, same difference. I thought it was a method because I'm not using 2.4 yet. The result is the same, other than that having it as a function instead of a method is another incon