Friday 13 September 2019

python - What does list[x::y] do?










I've been reading over some sample code lately and I've read quite a few websites but I just can't seem to get the query right to give me an answer I'm looking for. If anyone could help me out I'd appreciate it.


Answer



It slices



x[startAt:endBefore:skip]


if you use skip = 2, every other element the list beginning at startAt and ending at endBefore will be selected. [Remember: indices live BETWEEN list elements]



To see this, enter



x = range(100)


at the Python prompt. Then try these things



x[::2]
x[::3]
x[10:40:6]


and see what happens.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...