In Python, the sequence types are str, bytes, ByteArray, list, tuple, range. The so-called sequence, the description is orderly, you can do some specific operations through the index. First understand the two most important of the sequence objects: STR and list, and then explore the common operation of the next sequence object.
String: str
Text data in Python is handled by a Str object or string. A string is an immutable sequence of Unicode encodings (starting with Python3). You can create a string by using a sequence of characters in single quotation marks, double quotes, and three quotation marks. For example:
# Python 3.5.3 >>> test = "hello\tworld! ">>>Print(test) Hello world!>>>Test'hello\tworld!‘
#字符串中可以包含任何unicode编码中包含的字符. ' \ t ' is an escape character that appears as multiple spaces in the printout (depending on the operating environment)
# Direct input of variable name in interactive mode, display more to show how the string is stored in Python
>>> d = r'c:\windows\sys\table'>>>D'c:\\windows\\sys\\table'
# ' r ' prefix, ' raw ' abbreviation, will invalidate most of the escape
>>> sql ="""SELECT * from user where name= ' Tom '""">>>SQL"SELECT * from user where name= ' Tom '"
# Easy to use single and double quotes in three quotes
# with three quotes you can easily span multiple lines, and any whitespace character will be included in the string.
The string is iterative.
Listing: List
A list is a mutable sequence that is typically used to store a collection of homogeneous data, but any different types of data can be stored in a single list.
Lists can be created in different ways:
- Use an opposite bracket to represent an empty list: []
- Using a square bracket, the elements in parentheses are separated by commas:
[a, b, c]
- Using list parsing:[x for x in iterable]
- Use the type constructor of the list:
list()
orlist(iterable)
The constructor creates a list whose elements are exactly the same as the iterated objects that are parameters, and the elements are in the same order. An iterative object can be a sequence, a container that supports iterations, or an object that can be iterated. If an iterative object is a list, returns a copy of the list, functionally similar to iterable[:]. If there are no arguments, the constructor creates a new empty list, [].
>>> lst = [1] >>> cp = list (LST)>>> cp[, 2, 3]is Cpfalse
The following content is translated from file:///Library/Frameworks/Python.framework/Versions/3.5/Resources/English.lproj/Documentation/library/ Stdtypes.html#sequence-types-list-tuple-range
Common sequence Operations
The actions listed in the following table apply to all sequence types, whether they are variable or immutable sequences.
The operations in the table are sorted in ascending order of precedence. In the table, S and T are the same sequence type, n,j,i,k represents an integer, and x is an arbitrary object that satisfies any type and value imposed by S. In and not is the same priority as the comparison operation.
Operation |
Result |
Notes |
x in s |
True如果序列s中的某个元素等于x OtherwiseFalse |
(1) |
x not in s |
False If an element in the sequence s equals x, otherwiseTrue |
(1) |
s + t |
To concatenate s and T , return a new sequence
|
(6) (7) |
s * n Orn * s |
Equivalent to adding S itself n times |
(2) (7) |
s[i] |
The first element of the sequence s, indexed starting from 0 |
(3) |
s[i:j] |
Elements I to J of s , containing element I, not containing J elements |
(3) (4) |
s[i:j:k] |
A shard operation that takes an element of s from the I to J elements of a k element every |
(3) (5) |
len(s) |
The length of the s sequence |
|
min(s) |
The smallest element in the sequence s |
|
max(s) |
The largest element in the sequence s |
|
s.index(x[, i[, j]]) |
Index of the first X element appearing in S (after index of I and I, before J index) |
(8) |
s.count(x) |
X total number of occurrences in s |
|
The same list type supports comparisons. In fact, tuples and lists are performed by comparing the elements of the corresponding index position. That is, if two lists or tuple objects are equal, their lengths need to be the same and each element must be equal.
Comments:
in
and not in操作一般只用于普通的包含测试
, some special sequences, such as str, bytes, and ByteArray, also use them for sub-sequence testing:
" GG " inch " eggs " True
Values less than 0 are treated as 0 (producing an empty sequence with the same type as the S sequence). It is important to note that the elements in the sequence s are not copied, they are simply referenced several times.
>>> lists = [[]] * 3>>> lists[[], [], []]>>> lists[0].append (3)> >> lists[[3], [3], [3]]
[[]]是一个包含了一个空列表的元素的列表,所以通过
[[]] * 3
The resulting three elements all reference the same empty list. So modifying any one of the elements in the list actually modifies the same list. You can create a list that contains different list elements in the following ways:
for in range (3)]>>> lists[0].append (3)>>> lists[1].append (5) >>> Lists[2].append (7)>>> lists[[3], [5], [7]]
- If I or j is negative, then the index is relative to the end of the list s and is replaced by:
len(s) + i
orlen(s) + j。但是-0还是0.
The Shard operation from I to J defines a new sequence of items with index K i <= k < j。如果i或者j大于
len(s)
, using thelen(s)。如果i被省略,使用0。如果j被省略,使用len(s)。如果i大于等于j,那么返回的片段是空的。
(not to be continued)
Python sequence type: str, bytes, ByteArray, list, tuple, range