By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,772 Members | 906 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,772 IT Pros & Developers. It's quick & easy.

lists - append - unique and sorted

P: n/a
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

Jun 6 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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 bisect-module. 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
Jun 6 '07 #2

P: n/a
On 2007-06-06, rhXX <rh*****@gmail.comwrote:
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
Jun 6 '07 #3

P: n/a
Neil Cerutti wrote:
On 2007-06-06, rhXX <rh*****@gmail.comwrote:
>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 de-dupes the output.

As Diez has already said, 'use the bisect module' .

- Josiah
Jun 6 '07 #4

P: n/a
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 2007-06-06, rhXX <rh00...@gmail.comwrote:
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 de-dupes the output.

As Diez has already said, 'use the bisect module' .

- Josiah

Jun 6 '07 #5

P: n/a
On 2007-06-06, Josiah Carlson <jo************@sbcglobal.netwrote:
Neil Cerutti wrote:
>On 2007-06-06, rhXX <rh*****@gmail.comwrote:
>>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
de-dupes 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
Jun 6 '07 #6

P: n/a
On Jun 6, 10:26 am, rhXX <rh00...@gmail.comwrote:
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))

Jun 7 '07 #7

P: n/a

"Dan Bishop" <da*****@yahoo.comwrote in message
news:11**********************@m36g2000hse.googlegr oups.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()

Jun 7 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.