6.1 Sequence
6.1.2 Sequence type operator
Seq[ind]: Get the element labeled IND
SEQ[IND1:IND2]: Get subscript from Ind1 to ind2 elements collection
SEQ*EXPR: sequence repeats expr times
SEQ1+SEQ2: Connecting sequences seq1 and SEQ2
Obj in seq: determine if the obj element is contained in a SEQ
Obj not in SEQ: determine if the obj element is not included in the SEQ
--Member relationship operator (in, not in)
The member relational operators use to determine whether an element belongs to a sequence. For example, for a string type, it is to determine whether a character belongs to the string, and for a list and a tuple type, it represents whether an object belongs to the object sequence or not. The return value of the In/not in operator is generally true/false, which returns true if the member relationship is satisfied, otherwise False is returned.
--Slice operator ([], [:], [::])
I'm not very skilled at slicing, so read it carefully here.
>>> a = ' 123456789 ' >>> a[2:4] ' a[2 ' >>> a[2:-4] ' 345 ' >>> 3456789:] ' ' >>> a [-2:] ' a[:2 ' >>> ' ' 1234567 ' >>> a[:-2
LIST[A:B] This type: intercept the elements in list from start A to end of B. If a is empty, the default is 0, and if B is empty, the length of the sequence is the default.
>>> a[2::] ' 3456789 ' >>> a[::2] ' 13579 ' >>> a[2::3] ' 369 '
LIST[A::B] of this type: from the beginning of a, B takes the element once. If A or B is empty, the default is 0.
>>> A[:5:2] ' 135 ' >>> a[:5:-2] ' 97 '
LIST[:A:B] of this type: if B is positive, then the number of first A, every B number of values, if B is negative, then from the end of the sequence to a the element, the number of every B value.
6.1.3 built-in functions (BIFS)
The sequence itself is the concept of iteration, because the concept of iteration is derived from the generalization of sequences, iterators, or other objects that support iterative operations. Because Python's for loop can traverse all of the iteration types.
--Type conversion
List (ITER) converts an iterative object to a table
STR (obj) converts the Obj object to a string
A tuple (ITER) converts an iterative object to a tuple object
Why does Python not simply convert an object to another object? Once a Python object is created, we cannot change its identity or type. If you pass a list object to the list () function, a shallow copy of the object is created and then inserted into the new list.
A shallow copy is simply a copy of the object's index, not a re-establishment of an Object! If you want to completely copy an object (including recursion), you need to use a deep copy.
Built-in functions
Enumerate (ITER): Accepts an iterative object as a parameter and returns a enumerate object ( also an iterator) that generates a tuple of index values and item values for each element of ITER
Len (seq): Returns the length of the SEQ
Max (Iter,key=none) or Max (Arg0,arg1...,key=none): Return to ITER or (Arg0,arg1,...) The maximum value in the, if key is specified, the key must be a callback function that can be passed to the sort () method for comparison.
Min (iter, Key=none) or min (arg0, arg1 ... key=none): Returns the minimum value inside ITER, or returns (Arg0,arg2,...) the minimum value in the inside; If you specify a key, the key must be a callback function that can be passed to the sort () method for comparison .
Reversed (seq): takes a sequence as a parameter, returning an iterator that is accessed in reverse order
Sorted (ITER, Func=none, Key=none, Reverse=false): accepts an iterative object as an argument, returns an ordered list, and the optional parameters Func,key and reverse mean with list.sort () The parameters of the built-in function have the same meaning.
SUM (seq, init=0): Returns the sum of the SEQ and optional parameter init, which has the same effect as reduce (operator.add,seq,init)
zip ([It0, It1,... ItN]) : Returns a list whose first element is It0,it1,... The first element of these elements consists of a tuple, a second ..., and so on
>>> alist = [' A ', ' B ', ' C ']>>> blist = [1,2,3]>>> z = Zip (alist, blist) >>> z[(' A ', 1), (' B ', 2), (' C ', 3)]>>> for m,n in Zip (alist, blist): ... print m, N...A 1b 2c 3>>> for M,n in Enumer Ate (alist): ... print m, n ... 0 A1 B2 C
6.2 String
--creation and assignment of strings
Creating a string is as simple as trying to use a scalar.
>>> astring = ' Hello world! ' >>> anotherstring = ' Python is cool! ' >>> print Astringhello world!>>> anotherstring ' Python is cool! ' >>> s = str (range (4)) >>> s ' [0, 1, 2, 3] '
--access the value of the string (How to slice)
6.6 Built-in functions
String.capitalize () capitalizes the first character of a string
String.center (width) returns the center of the original string and fills the new string with a space of length width
String.count (str, beg = 0, end = Len (string)) returns the number of occurrences of STR within a string, if beg or end specifies the number of occurrences of STR in the specified range
String.endswith (obj, beg = 0, end = Len (string)) checks whether the string ends with obj, returns true if Beg or end specifies whether the specified range ends in obj, or False if
String.find (str, beg = 0, end = Len (string)) checks whether STR is contained in a string, if beg and end specify a range, the check is included in the range, or returns 1 if the index value is returned.
String.index (str, beg = 0, end = Len (string)) is the same as the Find method only if Str does not report an exception in string
String.isalnum () returns True if the string has at least one character and all characters are letters or numbers, otherwise false
String.isalpha () returns True if the string has at least one character and all characters are letters, otherwise false
String.isdigit () returns True if string contains only a number, otherwise false
String.islower () returns True if the string contains at least one case-sensitive character, and all of these characters are lowercase, otherwise false
String.isnumeric () returns True if the string contains only numeric characters, otherwise false
String.isspace () returns True if the string contains only spaces, otherwise false
Wait, no, it's all copied.
Built-in functions
CMP ():
>>> a = [' 123 ', ' abc ']>>> b = [' 123 ']>>> cmp (A, B) 1>>> cmp (B,A) -1>>> CMP (a,a ) 0
Len (), Max (), Min () omitted
Sorted (), reversed ():
>>> alist = [' 123 ', ' 456 ', ' qwe ', ' 2se ']>>> sorted (alist) [' 123 ', ' 2se ', ' 456 ', ' qwe ']>>> for I In reversed (alist): ..... print I ... 2seqwe456123
Enumerate (), zip () omitted
SUM ():
>>> alist = [1,2,3.0,5]>>> sum (alist) 11.0>>> sum (alist,3) 14.0
6.15 Special features of the list
Python core programming 6. Sequence: strings, lists, and tuples