Appendix 2
Selected standard type methods and
operations
Operations common to strings, lists and tuples
The table below lists most of the functions that may be applied to Python sequence
(ordered) collection types. For Python 2 the sequence types are buffer, bytearray, list, str,
tuple, unicode and xrange. For Python 3 these types are bytearray, bytes, memoryview,
list, range, str and tuple.
1
Operation
Description
Example
x in seq
x not in seq
Determines whether an item is or is not in a
collection, giving True or False
accordingly.
myList =
[3,1,4,1,5,9]
2 in myList #
False
8 not in myList
# True
seqA + seqB
Generates a new collection which is the
combination of two collections of the same
type, in order.
tupleA =
('G','C')
tupleB = ('A',
'T')
tupleA + tupleB
('G', 'C', 'A',
'T')
seq * n
n * seq
Generates a new collection based by
repeating the items of a collection a
number of times.
'AB' * 4 #
'ABABABAB'
5 * [0] #
[0,0,0,0,0]
seq[i]
Accesses an item in an ordered collection
by using a positional index, which starts
from zero.
text = 'Banana'
text[0] # 'B'
nums =
[1,4,9,16,25]
nums[2] # 9
seq[i:j]
Generates another, usually smaller
collection, by accessing a range of items
from an ordered collection, from a starting
positional index, up to but not including a
second index. If unspecified, the starting
index defaults to zero and the end defaults
to the end of the collection. Negative index
numbers count from the end.
text = 'Banana'
text[1:5] #
'anan'
x =
[1,4,9,16,25]
x[2:4] #
[9,16]
x[:4] #
[1,4,9,16]
x[1:] #
[4,9,16,25]
x[1:-1] #
[4,9,16]
seq[i:j:k]
Generates another, usually smaller
collection, by accessing a range of items
from an ordered collection, using positional
indices (see above) and a step size to skip
certain indices. Step may be negative for
reverse direction.
x =
[1,4,9,16,25]
x[0:5:2] #
[1,9,25]
x[::2] #
[1,9,25]
x[::-1] #
[25,16,9,4,1]
len(seq)
Gives the total number of items in the
collection; its size.
len('Banana') #
6
len((1,4,9,16))
# 4
len([]) # 0
min(seq)
Retrieves the item from a collection that
has the smallest value. This does not work
on multi-dimensional NumPy arrays.
x = [10, 5, 1,
10, 3]
min(x) # 1
min('ABCDE') #
'A'
max(seq)
Retrieves the item from a collection that
has the largest value. This does not work
on multi-dimensional NumPy arrays.
x = (10, 5, 1,
10, 3)
max(x) # 10
max('abcdefgh')
# 'h'
seq.index(item)
Retrieves the (first) positional index at
which an item is found in a collection.
Assumes the item is present and gives a
ValueError if not.
text = 'Banana'
text.index('a')
# 1
x =
[10,5,1,10,3]
x.index(3) # 4
seq.count(item)
Counts the number of occurrences of a
given item within a collection.
text = 'Banana'
text.count('a')
# 3
String methods
The table below describes selected methods that relate to standard Python textual string
objects, which you would call using the dot notation string.method(arg). Separate
descriptions for string formatting (both old and new style) and the regular expression
module re are given in
Appendix 4
and
Appendix 5
respectively. Note that most objects
can be converted to a string representation
2
with str(x).
It should be noted that there are big changes in Python between version 2 and version 3
with regard to handling Unicode. In Python 3 all strings are Unicode, so there is no need
for unicode(x) or u” syntax;
3
any ‘\u’ escaped codes in a string will naturally be
recognised as Unicode characters. Also, the binary data type is introduced, which may be
created with bytes(), which is immutable, or bytearray(), which is mutable. The bytes type
may be used for general binary data, which may include binary encoded Unicode. Before
Python 3 the normal string type was used for binary data.
Do'stlaringiz bilan baham: |