这可能是最透彻的Python slice 分片教程

A Quick Guide to Slicing in Python – Become a Python Ninja

Slicing is an incredibly useful and powerful feature of python. It gives you to ability to manipulate sequences with simple and concise syntax. Slicing has more uses than I can think of or list here, but some of many useful applications include string manipulation and various mathematical uses; when using NumPy you will encounter slicing a lot.

UNDERSTANDING SLICING

Slicing has incredibly simple syntax; after a sequence type variable inside square brackets you define a start point, an end point and a step, separated by colons like so; var[start:end:step]. However it doesn’t work the way you might first assume it to. For example if you choose the starting point to be 0 and the end point to be 5, the slice will include indexes 0, 1, 2, 3 and 4. The index 5 will not be included.

One way to remember this is that the start point is inclusive and the end point is exclusive. In other words the slice will be from the start point up to but not including the end point. For the more visual folks another way is to to picture a wall before each value in the sequence. Then the start and end points reference these walls, not the values behind them. The slice will be between these walls, which of course will not include the value behind the end wall because we only go up to the wall and don’t pass it.

slicing_diagram

The diagram above shows how to picture sequence indexes and the values they reference. Thinking this way will make understanding slicing a lot easier.

A BASIC SLICING EXAMPLE

Throughout this post we will be using the variables defined in the below example but to avoid clutter they will only be included in this first example. The output of each statement will be included in a comment after it starting with ‘>>>’.

 

1

2

3

4

5

6

7

l = [23, 42, 96, 7, 84, 99, 54, 1]

t = (2, 85, 64, 129, 92, 84, 1, 33)

s = 'Hello internet!'

# Basic slicing.

print(l[4:7]) # >>>[84, 99, 54]

print(t[4:7]) # >>>(92, 84, 1)

print(s[4:7]) # >>> 'o i'

The above lines of code take a slice from the 4th element in the sequence up to but not including the 7th element. Note that strings can be sliced as well as lists and tuples.

OMITTING START/END POINTS

When specifying a slice you can omit start and end points. What this does is set the start point to 0 if you omit the start point and set the end point to end of the sequence, including the last item if you omit the end point.

 

1

2

3

4

# First five.

print(l[:5]) # >>>[23, 42, 96, 7, 84]

print(t[:5]) # >>>(2, 85, 64, 129, 92)

print(s[:5]) # >>>'>Hello'

The above examples makes a slice of the first 5 elements in the sequence. They omit the start point of the slice and so therefore start from the beginning.

 

1

2

3

4

# Last five.

print(l[-5:]) # >>>[7, 84, 99, 54, 1]

print(t[-5:]) # >>>(129, 92, 84, 1, 33)

print(s[-5:]) # >>>'rnet!'

This example does the opposite and makes a slice of the last 5 elements. This time the end point is omitted and so the slice is taken to the end of the sequence.

STEPPING

Another useful feature of slicing is stepping. The step of your slice defines how it ‘steps’ through the sequence your are slicing. For example a step of 5 would make it jump five elements with each step and a step of -1 would make it step backwards through the sequence.

 

1

2

3

4

# Every other.

print(l[::2]) # >>>[23, 96, 84, 54]

print(t[::2]) # >>>(2, 64, 92, 1)

print(s[::2]) # >>>Hloitre!

The above example defines a step of 2 which produces a slice of every other element; every evenly indexed element. If you wanted to take a slice of every oddly indexed element then you would input 1 as the start point.

 

1

2

3

4

# Reverse order.

print(l[::-1]) # >>>[1, 54, 99, 84, 7, 96, 42, 23]

print(t[::-1]) # >>>(33, 1, 84, 92, 129, 64, 85, 2)

print(s[::-1]) # >>>!tenretni olleH

This example reverses through the sequence and produces a slice containing every element but in reverse order.

Note that in the two above examples both the start and end point of the slice is omitted. This produces a slice of the whole sequence from start to finish. Normally this would be useless, however when combined with a step it provides a useful way of manipulating a whole sequence.

And that’s it for slicing! Python’s slicing is an incredibly useful function but also incredibly easy to use once you have understood the way that indexes are used. Hopefully now you are well on your way to ninja level slicing skills in Python.

Make sure you share this post so others can read it as well and don’t forget to follow me on Twitter, add me on Google+ and subscribe to this blog by email to make sure you don’t miss any posts that are useful to you.

Don't forget to share and follow!

Remember, don't forget to share this post so that other people can see it too! Also, make sure you subscribe to this blog's mailing list, follow me on Twitter and add me on Google+ so that you don't miss out on any useful posts!

I read all comments, so if you have something to say, something to share or questions and the like, leave a comment below!

转自:Data Dependence 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值