[Tutor] sorting in python
Python
python at venix.com
Mon Jun 12 17:44:50 CEST 2006
On Mon, 2006-06-12 at 11:26 -0400, Kermit Rose wrote:
> # to insert 3 between 2 and 4 in
>
> B = [1,2,4,5]
>
> B.append(B[3:3])
>>> B[3:3]
[]
>>> B[3:4]
[5]
>>> B[0:1]
[1]
>>> B[:2]
[1, 2]
>>> B.append(B[3:3])
>>> B
[1, 2, 4, 5, []]
>
> # I expected B[4] to have the value 5 at this point.
> # It is empty. Why?
You appended an empty list.
Note that slice notation returns a list, so you would more commonly use
B.extend(B[0:3]) to avoid nested lists.
The number before the : is the index to include from
The number after the : is the index to exclude
Slices are half-open intervals. The lower-bound is included while the
upper-bound is excluded.
http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF
Provides an excellent justification for this approach
(cited recently on this list)
--
Lloyd Kvam
Venix Corp
More information about the Tutor
mailing list