Getting Started with Python [8-1] slice | slice of list, reverse slices, string sections

1, on the list sliced

Take a portion of list elements are very common operation. For example, a list follows:

>>> L = ['Adam', 'Lisa', 'Bart', 'Paul']

Before taking three elements, how we should do?

Awkward:

>>> [L[0], L[1], L[2]]
['Adam', 'Lisa', 'Bart']

The reason is awkward because of the extended bit, take the first N elements Meizhe.

Take the first N elements, that is, index 0- (N-1) elements, and repeated with:

>>> r = []
>>> n = 3
>>> for i in range(n):
...     r.append(L[i])
... 
>>> r
['Adam', 'Lisa', 'Bart']

This operation is often taken to specify the index range, with the cycle is very cumbersome, and therefore, Python provides a slice (the Slice) operator, such an operation can be greatly simplified.

Corresponding to the above problem, take the first three elements, a complete line of code sections:

>>> L[0:3]
['Adam', 'Lisa', 'Bart']

L [0: 3] represents a start index taken from 0, until the index 3, but not including the index 3. That index 0,1,2, exactly three elements.

If the first index is 0, may further be omitted:

>>> L[:3]
['Adam', 'Lisa', 'Bart']

1 may start, two elements removed from the index:

>>> L[1:3]
['Lisa', 'Bart']

Only one: showing from start to finish:

>>> L[:]
['Adam', 'Lisa', 'Bart', 'Paul']

Therefore, L [:] actually copied out a new list.

The slicing operation may also specify the third parameter:

>>> L[::2]
['Adam', 'Bart']

The third parameter every N take one of the above L [:: 2] will remove one of each of two elements, i.e. a spacer take a.

Into the tuple list, the same as the slicing operation, slices only the result has become a tuple.

task:

range () function can create a number of columns:

>>> range(1, 101)
[1, 2, 3, ..., 100]

Please use slices, remove:

  1. Before the number 10;
  2. 3 basis multiples;
  3. Not more than 50 5 fold.

Code:

L = range(1, 101)
print L[-10:]
print L[-46::5]

Here Insert Picture Description

2, reverse sliced

For List, since Python supports L [-1] taking the inverse of the first element, it also supports the reciprocal of slices, try:

>>> L = ['Adam', 'Lisa', 'Bart', 'Paul']

>>> L[-2:]
['Bart', 'Paul']

>>> L[:-2]
['Adam', 'Lisa']

>>> L[-3:-1]
['Lisa', 'Bart']

>>> L[-4:-1:2]
['Adam', 'Bart']

Remember the penultimate element of the index is -1.
Reverse slice contains the starting index, the index does not include the end.



task:

Using descending slicing 1-- extraction column number 100:

  • Finally, number 10;

  • Finally 10 multiples of five.

Code:

L = range(1, 101)
print L[-10:]
print L[-46::5]

Here Insert Picture Description

3, the slice string

String 'xxx' and Unicode strings u'xxx 'may be regarded as a list, each element is a character. Therefore, the string can also use the slicing operation, the operating result is still just a string:

>>> 'ABCDEFG'[:3]
'ABC'
>>> 'ABCDEFG'[-3:]
'EFG'
>>> 'ABCDEFG'[::2]
'ACEG'

In many programming languages, it offers a lot of variety for a string interception function, in fact, the purpose is to string sections. Python is not taken for the function string, only a slice operation can be completed very simple.

task:

There is a string methods upper () can become characters in capital letters:

>>> 'abc'.upper()
'ABC'

But it will all letters are capitalized. Please design a function that takes a string and returns only the first letter capitalized string.

Tip: string operations using slicing simplified.

def firstCharUpper(s):
    return s[:1].upper() + s[1:]

print firstCharUpper('hello')
print firstCharUpper('sunday')
print firstCharUpper('september')

Here Insert Picture Description

Published 20 original articles · won praise 0 · Views 408

Guess you like

Origin blog.csdn.net/yipyuenkay/article/details/104038533