what is this?

E

Eric Price

Hello;
I'm studying some code examples from the python cookbook site. I came across
this:

def colsplit(l, cols):
rows = len(l) / cols
if len(l) % cols:
rows += 1
m = []
for i in range(rows):
m.append(l[i::rows])
return m

What I'd like to know is what is the double colon? What does it do?
m.append(l[i::rows])

Thanks,
Eric

_________________________________________________________________
Type your favorite song.  Get a customized station.  Try MSN Radio powered
by Pandora. http://radio.msn.com/?icid=T002MSN03A07001
 
H

Huayang Xia

Sequence slicing [starting-at-index : but-less-than-index [ : step]].
Start defaults to 0, end to len(sequence), step to 1.
So l[i::rows] means: slicing start from i, ending with len(l) and step
with rows. So function colsplit(l, cols) returns a list of sequence
with conversion of:

Assume cols = 4:
From:

ABCD
ABCD
ABCD
AB

To:
AAAABBBBCCCDDD
 
P

Paul Watson

Eric said:
Hello;
I'm studying some code examples from the python cookbook site. I came
across this:

def colsplit(l, cols):
rows = len(l) / cols
if len(l) % cols:
rows += 1
m = []
for i in range(rows):
m.append(l[i::rows])
return m

What I'd like to know is what is the double colon? What does it do?
m.append(l[i::rows])

Thanks,
Eric

http://docs.python.org/tut/tut.html
http://docs.python.org/tut/node5.html#SECTION005140000000000000000
http://docs.python.org/ref/slicings.html

Probably most helpful to you is:

http://developer.mozilla.org/es4/proposals/slice_syntax.html
 
P

Paul Watson

Paul said:
Eric said:
Hello;
I'm studying some code examples from the python cookbook site. I came
across this:

def colsplit(l, cols):
rows = len(l) / cols
if len(l) % cols:
rows += 1
m = []
for i in range(rows):
m.append(l[i::rows])
return m

What I'd like to know is what is the double colon? What does it do?
m.append(l[i::rows])

Thanks,
Eric

http://docs.python.org/tut/tut.html
http://docs.python.org/tut/node5.html#SECTION005140000000000000000
http://docs.python.org/ref/slicings.html

Probably most helpful to you is:

http://developer.mozilla.org/es4/proposals/slice_syntax.html

Sorry. Probably most helpful to you is:

http://docs.python.org/lib/built-in-funcs.html

slice( [start,] stop[, step])
Return a slice object representing the set of indices specified by
range(start, stop, step). The start and step arguments default to None.
Slice objects have read-only data attributes start, stop and step which
merely return the argument values (or their default). They have no other
explicit functionality; however they are used by Numerical Python and
other third party extensions. Slice objects are also generated when
extended indexing syntax is used. For example: "a[start:stop:step]" or
"a[start:stop, i]".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top