List Problem

Robert Lehmann stargaming at gmail.com
Wed Dec 10 01:02:20 EST 2008


On Tue, 09 Dec 2008 21:40:08 -0800, dongzhi wrote:

> I have one problem for List. Like that:
> 
> format='just "a" ""little"" test'
> part = format.split('"')
> print part
> 
> the result is : ['just ', 'a', ' ', '', 'little', '', ' test']
> 
> the list part have 7 element.
> 
> If I execute part[1], I have got  'a'. If I execute part[2], I have got
> ' '. But, if I execute part[1::2], I have got ['a', '', '']. I don't
> know why. Please tell me why.

You're slicing your list with the arguments "start at 1, stop at the end, 
using a step size of 2." It's basically the same as ``part[1], part[1+2], 
part[1+2+2], ...``. Perhaps you wanted to do ``part[1:3]`` (meaning 
"start at 1, stop before 3").

See the Python Reference for details.
http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-
list-tuple-buffer-xrange
http://docs.python.org/reference/expressions.html#id8

HTH,

-- 
Robert "Stargaming" Lehmann



More information about the Python-list mailing list