




Python Programming Language









lists  append  unique and sorted
hi, can i append a item to a list using criterias:  UNIQUE  if there already exist don't append and/or  SORTED  INSERT in the correct place using some criteria? tks in advance
rhXX wrote: > hi, > can i append a item to a list using criterias: >  UNIQUE  if there already exist don't append >  SORTED  INSERT in the correct place using some criteria?
Both can be accomplished using the bisectmodule. It will give you the leftmost/rightmost insertion point for a given item and a list, and then you have to see if it is contained in between (or something along these lines, I hope you get the gist of it) Diez
On 20070606, rhXX <rh00@gmail.com> wrote: > hi, > can i append a item to a list using criterias: >  UNIQUE  if there already exist don't append
Consult the Python Docs about sets. > and/or >  SORTED  INSERT in the correct place using some criteria?
Consult the Python Docs about the heapq module.  Neil Cerutti Beethoven wrote fewer symphonies than Haydn and Mozart because he wrote longer, and besides he went death. Music Lit Essay
Neil Cerutti wrote: > On 20070606, rhXX <rh00 @gmail.com> wrote: >> and/or >>  SORTED  INSERT in the correct place using some criteria? > Consult the Python Docs about the heapq module.
Heaps (as produced by heapq) are not sorted. This will not produce correct results unless one then pops everything and dedupes the output. As Diez has already said, 'use the bisect module' .  Josiah
On Jun 6, 6:35 pm, Josiah Carlson <josiah.carl@sbcglobal.net> wrote: ok, tks to all for ur help and comments!!!
> Neil Cerutti wrote: > > On 20070606, rhXX <rh00 @gmail.com> wrote: > >> and/or > >>  SORTED  INSERT in the correct place using some criteria? > > Consult the Python Docs about the heapq module. > Heaps (as produced by heapq) are not sorted. This will not produce > correct results unless one then pops everything and dedupes the output. > As Diez has already said, 'use the bisect module' . >  Josiah
On 20070606, Josiah Carlson <josiah.carl@sbcglobal.net> wrote: > Neil Cerutti wrote: >> On 20070606, rhXX <rh00 @gmail.com> wrote: >>> and/or >>>  SORTED  INSERT in the correct place using some criteria? >> Consult the Python Docs about the heapq module. > Heaps (as produced by heapq) are not sorted. This will not > produce correct results unless one then pops everything and > dedupes the output.
i agree that using bisect and inserting manually clearly meets the stated requirements, while there isn't enough information to know if a heapq will meet his requirements. Thanks for the correction.  Neil Cerutti In my prime I could have handled Michael Jordan. Of course, he would be only 12 years old. Jerry Sloan
 Neil Cerutti <horp@yahoo.com> wrote: > i agree that using bisect and inserting manually > clearly meets > the stated requirements, while there isn't enough > information to > know if a heapq will meet his requirements. > Thanks for the correction.
If the OP is still reading, don't disregard heapq too quickly. Heapq will perform much better for a large enough data set, if the only thing that you eventually want to do is occasionally pop off the first item or items in the list. One could even argue that it's a pretty rare situation that you go through the trouble of keeping a list sorted, only to occasionally randomly access the data. One more caveatapart from the more constrained semantics, heaps have the disadvantage of actually being slower for a small enough dataset. I haven't measured this particular scenario, but any complicated data structure usually loses out to a simple data structure for small data sets, but as you get larger and larger data sets, the more sophisticated data structure begins to have big wins. A typical use of heapq is for keeping a priority queue. See sched.py in the standard library for a pretty simple and well commented example. ___________________________________________________________________________ _________ Boardwalk for $500? In 2007? Ha! Play Monopoly Here and Now (it's updated for today's economy) at Yahoo! Games. http://get.games.yahoo.com/proddesc?gamekey=monopolyherenow
On Jun 6, 10:26 am, rhXX <rh00@gmail.com> wrote: > hi, > can i append a item to a list using criterias: >  UNIQUE  if there already exist don't append > and/or >  SORTED  INSERT in the correct place using some criteria? > tks in advance
If you don't need the list to be sorted until you're done building it, you can just use: lst = sorted(set(lst))
"Dan Bishop" <danb @yahoo.com> wrote in message news:1181181710.879767.234090@m36g2000hse.googlegroups.com...  If you don't need the list to be sorted until you're done building it,  you can just use:   lst = sorted(set(lst)) ?? looks same as lst.sort()
You missed that the OP wants only unique values from the original list. Tim Delaney





