目录
Sequence Overview
The main difference between lists and tuples is that you can change a list, but you can’t change a tuple.
This means a list might be useful if you need to add elements as you go along, while a tuple can be useful if,
for some reason, you can’t allow the sequence to change. Reasons for the latter are usually rather technical,
having to do with how things work internally in Python. That’s why you may see built-in functions returning
tuples. For your own programs, chances are you can use lists instead of tuples in almost all circumstances.
序列中可以包含序列
sequences can contain other sequences, too, so you could make a list of such persons, which would be
your database.
>>> edward = ['Edward Gumby', 42]
>>> john = ['John Smith', 50]
>>> database = [edward, john]
>>> database
[['Edward Gumby', 42], ['John Smith', 50]]
python has a basic notion of a kind of data structure called a container, which is basically any object
that can contain other objects. the two main kinds of containers are sequences (such as lists and tuples) and
mappings (such as dictionaries). While the elements of a sequence are numbered, each element in a mapping
has a name (also called a key).
Common Sequence Operations
These operations include indexing, slicing, adding, multiplying, and checking for membership. In addition, Python has built-in functions for finding the length of a sequence and for finding its largest and smallest elements.
Indexing
When you use a negative index, Python counts from the right, that is, from the last element. The last element
is at position –1.
>>> greeting = 'Hello'
>>> greeting[0]
'H'
>>> greeting[-1]
'o
This is called indexing.
Slicing
Just as you use indexing to access individual elements, you can use slicing to access ranges of elements. You
do this by using two indices, separated by a colon.
The first index is the number of the first element you want to include. However, the last index is the number
of the first element after your slice. Consider the following:
>>> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> numbers[3:6] [4, 5, 6]
>>> numbers[0:1] [1]
前闭后开
In short, you supply two indices as limits for your slice, where the first is inclusive and the second is exclusive.
A Nifty Shortcut
当切片的结束的索引后面再没有元素时,则会把未尾的元素取出来
>>> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> numbers[8:10]
[9, 10]
>>> numbers[8:11]
[9, 10]
>>> numbers[8:9]
[9]
>>> num = numbers
>>> num[-3:0]
[]
>>> num[-3:]
[8, 9, 10]
extracts the domain name
# Split up a URL of the form http://www.something.com
url = input('Please enter the URL:')
domain = url[11:-4]
print("Domain name: " + domain)
Here is a sample run of the program:
Please enter the URL: http://www.python.org
Domain name: python
Longer Steps
步长参数默认情况下为1,可以设置为其它参数
When slicing, you specify (either explicitly or implicitly) the start and end points of the slice. Another parameter,
which normally is left implicit, is the step length. In a regular slice, the step length is one, which means that the
slice “moves” from one element to the next, returning all the elements between the start and end.
>>> numbers[0:10:1]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> numbers[0:10:2]
[1, 3, 5, 7, 9]
numbers[3:6:3]
[4]
You can still use the shortcuts mentioned earlier. For example, if you want every fourth element of a
sequence, you need to supply only a step size of four
>>> numbers[::4]
[1, 5, 9]
Naturally, the step size can’t be zero—that wouldn’t get you anywhere—but it can be negative, which means
extracting the elements from right to left.
>>> numbers[8:3:-1]
[9, 8, 7, 6, 5]
>>> numbers[10:0:-2]
[10, 8, 6, 4, 2]
>>> numbers[0:10:-2]
[]
>>> numbers[::-2]
[10, 8, 6, 4, 2]
>>> numbers[5::-2]
[6, 4, 2]
>>> numbers[:5:-2]
[10, 8]
>>> numbers[::-1]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
Getting things right here can involve a bit of thinking. As you can see, the first limit (the leftmost) is still
inclusive, while the second (the rightmost) is exclusive. When using a negative step size, you need to have a
first limit (start index) that is higher than the second one. What may be a bit confusing is that when you leave
the start and end indices implicit, Python does the “right thing”—for a positive step size, it moves from the
beginning toward the end, and for a negative step size, it moves from the end toward the beginning.