Python Learning Diaries[1]:List & Tuple
![Python Learning Diaries[1]:List & Tuple](/wp-content/themes/uazohtwo/css/images/theme01.png)
#1. Common Sequence Operations
Indexing
Start counting from 0 and end with -1
>>> a=[0,1,2,3,4,5,6,7,8,9]
>>> a[0]
0
>>> a[-1]
9
Slicing
Slicing is done by two colon-separate indices.
The first index is the number of the first element you want to include. But the last index is the number of the first element after your slice.
slicedString = aString[beginIndex:endIndex]
>>> a=[0,1,2,3,4,5,6,7,8,9]
>>> a[0:1]
[0]
>>> a[0:5]
[0, 1, 2, 3, 4]
If the slice continues to the end of the sequence,you may simply leave out the last index.
>>> a[5:]
[5, 6, 7, 8, 9]
>>> a[5:10]
[5, 6, 7, 8, 9]
#Longer steps
The implicit step length is 1,which means that the slice ‘moves’ from one elements to the next.
The step length can’t be zero, But it can be negative, which means extracting the element from right to the left.
>>> a[::1]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
Adding Sequence
You can concatenate sequences by using plus operator. But only the same-type sequences can be concatenated.
>>> [1,2,3]+[4,5,6]
[1, 2, 3, 4, 5, 6]
>>> [4,5,6]+’11’
Traceback (most recent call last):
File “<pyshell#10>”, line 1, in <module>
[4,5,6]+’11’
TypeError: can only concatenate list (not “str”) to list
Multiplying
Multiplying a sequence with a number x creating a new sequence where the original sequence is repeated x times.
>>> ‘python’*3
‘pythonpythonpython’
Membership
To check whether a sequence contained a value, you can use in operator. It returns Boolean value(True or False)
>>> ‘aa’ in ‘aabc’
True
>>> 1 in [2,3,4]
False
Length/Maximum/Minimum
The Build-in functions len,max,min
>>> max([1,2,3,4,5])
5
>>> max(1,2,3,4,5)
5
>>> len(‘123′)
3
>>> min(‘abcd’)
‘a’
Caution: max and min can be called with numbers directly
#2. List
List is mutable—you can change it’s contents
list()
Generating a list with any-type sequence
>>> list(‘1234′)
[‘1′, ‘2’, ‘3’, ‘4’]
>>> list([1,2,3])
[1, 2, 3]
Item Assignments
>>> x=[1,2,3]
>>> x
[1, 2, 3]
>>> x[1]=0
>>> x
[1, 0, 3]
Caution: Cannot assign to a position doesn’t exist
Deleting Elements
You can simply use del statement to delete elements
>>> a=[1,2,3,4]
>>> del a[3]
>>> a
[1, 2, 3]
Slice Assignments
You may replace the slice with a sequence whose length is different from that of the original
>>> a=[1,2,3]
>>> a[0:1]=[0,0,0,0,0]
>>> a
[0, 0, 0, 0, 0, 2, 3]
Slice assignments can even insert new elements without replace original ones.
>>> a=[1,2,3]
>>> a[1:1]=[‘a’,’a’,’a’]
>>> a
[1, ‘a’, ‘a’, ‘a’, 2, 3]
And you can do the reverse to delete a slice.
List Methods
append
The append method is used to append an object to the end of a list.
Caution: The append method modifies the original list directly.
count
The count method counts the occurrences of an element in a list.
extend
The extend method allows you to append several values at once. In other words, the original list has been extended by the other one. And it is an in-place method.
index
The index method is used for searching lists to find the index of the first occurrence of a value.
insert
The insert method is used to insert an object into a list.
pop
The pop method will remove an element (by default, the last one) from the list and returns it.
remove
The remove method is used to remove the first occurrence of a value. It modifies the list, but returns nothing.
Caution: It is a “nonreturning in-place changing” method.
reverse
The reverse method reverses the element in the list. It modifies the list, but returns nothing too.
sort
The sort method is used to sort lists in place.
Another way of getting a sorted copy of the list is using sorted function.
Advanced Sorting
If you want to have your elements sorted in a specific manner, you can supply a compare function as a parameter to sort.
The sort method has two other optional arguments: key and reverse. These two arguments need to be specified by name.
#3 Tuple
Tuples are sequences, just like lists.The only difference is that tuple is immutable.
The tuple synatx is simple–if you separate some values with commas, you automatically have a tuple.
Tuple may also be enclosed in parenthese.
Caution: You must to include a comma, even though there is only one value.
>>> 42
(42)
>>> (42,)
(42,)
The tuple function
The tuple function is pretty much the same way as list.
It takes one sequence argument and converts it to a tuple.
发表评论