Python Learning note 6:python sequence

Source: Internet
Author: User
Tags control characters shallow copy expression engine

1. Sequence
The members are arranged in an orderly manner, and the type of one or more of its members can be accessed through the subscript offset as a sequence.
(1) standard type operator
(2) Sequence type operator

    • Member relationship operator (In,not in) object [not] in sequence
    • Connection operator (+) SEQ1+SEQ2
    • Repeat operator (*) Seq*copies_int
    • Slice operator ([],[:],[::])
      Seq[index] (0<=index<=len (seq)-1 | |-len (SEQ) <=index<=-1)
      Seq[starting_index:ending_index] (excluding elements corresponding to the end index)
      The start and end index values can exceed the length of the string.
    • Extended Slice operation ([:: Step parameter])

(3) built-in function (BIF)
Iterations are generalized from sequences, iterators, or other objects that support iterative operations. Many built-in functions that only support sequences as parameters now also begin to support iterators or class iterators. These types are collectively referred to as " iterative objects ."
(a) type conversion (Factory function)
List () str () tuple () takes the object as an argument and copies its contents (shallow) to the newly generated object.

    • List (ITER) converts an iterative object to a table
    • STR (obj) converts the Obj object to a string (the string representation of the object)
    • Unicode (obj) converts an object to a Unicode string (using the default encoding)
    • The basestring () abstract Factory function provides only the parent class for STR and Unicode functions, cannot be instantiated, and cannot be called
    • A tuple (ITER) converts an iterative object to a tuple object

(b) Operable
Len () reversed () sum () accepts only the sequence as a parameter, and Max () min () can also accept a parameter list

    • Enumerate (ITER) (2.3) takes an iterative object as a parameter and returns a enumerate object (also an iterator) that generates a tuple of index 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) (2.5 starts to support the keyword parameter) returns to ITER or (Arg0,arg1,...) Maximum value, 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) (2.5 start support keyword parameter) return to ITER or (Arg0,arg1,...) Minimum value, if key is specified, the key must be a callback function that can be passed to the sort () method for comparison
    • Reversed (seq) (2.4) takes a sequence as a parameter and returns an iterator that is accessed in reverse order
    • Sorted (Iter,func=none,key=none,reverse=false) takes an iterative object as a parameter, returns an ordered list, the meaning of the optional parameter Func,key,reverse and List.sort () The parameters of the built-in function have the same meaning
    • SUM (seq,init=0) (2.3) 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]) (2.0 join, 2.4 strengthen) returns a list whose first element is a tuple of it0,it1,..., ItN The first element of these elements, and the second ... And so on

2. String
The single and double quotes in Python work the same.
A string is a direct quantity or a scalar.
A string is an immutable type and cannot change only one character of a string or a substring. Changing a string must be done in a way that creates a new string.
Python has three types of strings: the usual meaning of the string (str) and the Unicode string (Unicode) are actually subclasses of the abstract class basestring. Basestring is an abstract class and cannot be instantiated.
Empty or delete a string: astring= "or del astring

3. Strings and Operators
(1) standard type operator
(2) Sequence operator slices ([] and [:])

    • Forward index: 0,1,2,3
    • Reverse Index: -4,-3,-2,-1
    • Default index: If the start index or the end index is not specified, the first index value of the string and the last index value are the default values, respectively.

Both the start and end indexes do not specify that the entire string is returned.
(a) member operator (in or not)
The member operator is used to determine whether a character or a substring appears in another string, returns True if it occurs, or false. The decision whether a string contains another string is done by the find () or the index () function (RFind ()/rindex ()).
The string module contains a predefined string. (Note: It is recommended not to use the string module, performance)
(b) connector (+)
Joins a normal string with a Unicode string, and the normal string is converted to a Unicode string.

    • Run-time string connection.

Use "%s%s" to format the operator or put all the strings in the list, and then use the Join method to connect, as little as possible with the "+" connection string.

‘%s %s‘ % (‘Spanish‘,‘Inquisition‘)>>>s=‘‘.join((‘Spanish‘,‘Inquisition‘))
    • Compile-time string connection
      Python allows several strings to be written together in the source code to construct a new string.
foo=“hello”“world!”f=urllib.urlopen("http://"       #protocol                            "localhost"#hostname

(c) Repeat operator (*)

>>>‘Ni!‘3‘Ni!Ni!Ni!‘

4. Operators that apply only to strings
(1) format operator (%)

    • %c ASCII character
    • %r (2.0) Prioritize string conversions with the repr () function
    • %s takes precedence over string conversion with the STR () function
    • %d/%i turns into signed decimal numbers
    • %u turns into unsigned decimal number
    • %o Turn unsigned octal number
    • %x/%x to unsigned hexadecimal number
    • %e/%e turn into scientific counting method
    • %f/%f into floating-point type
    • Shorthand for%g/%g%e and%f/%e and%f (converted to%e or%f/%e or%f depending on number size)
    • % OUTPUT%

Python supports input parameters in two formats:

    • Meta-group
‘dec:%d/oct:%#o’% (num,num)  
    • Dictionary
 ‘%(howmany)%(lang)s’% {‘lang‘:‘Python‘,‘howmany‘:‘3‘}

The following are the formatting operator auxiliary directives:

    • * Define width or decimal point accuracy
    • -Used to align Left
    • + Show plus sign (+) in front of positive number
    • Show spaces in front of positive numbers
    • # 0 (' 0 ') in front of octal number, display ' 0x ' or ' 0X ' before hexadecimal number (depending on ' x ' or ' x ') >>> '% #X '% 108
    • 0 The number shown is preceded by ' 0 ' instead of the default space
    • % ' percent ' output a single '% '
    • (VAR) mapping variable (dictionary parameter)
    • M.N m is the smallest total width displayed, and n is the number of digits after the decimal point (if applicable)

All Python objects have a string representation (represented by the repr () function or the STR () function). The print statement automatically calls the STR () function for each object.
(2) string template
String formatting operator Disadvantage: Not intuitive enough, there will be missing conversion type symbol error, you need to remember the conversion type parameters and other details.

>>>fromimport Template(2.4)>>>s = Template(‘${howmany} ${lang}‘)>>>print s.substitute(lang=‘Python‘,howmany=3)

The string module contains the substring () and Safe_substitute () methods:

    • Substitute () more rigorous, in key missing case will report Keyerror exception;
    • The Safe_substitute () method displays the string directly intact when the key is missing.

String template Advantages over string formatting operators: Don't remember all the details (such as type symbols)
(3) Primitive string operator (R/R) (1.5)

>>>printr‘\n‘   #r/R必须紧靠在第一个引号前面\n

(4) Unicode string operator (u/u) (1.6)

>>>printu‘abc‘>>>printur‘Hello\n Worlld!‘

5. Built-in function
(1) standard type function
CMP () is compared against the ASCII code values.
(2) Sequence type function

    • Len ()
    • Max ()/min ()
    • Enumerate ()
    • Zip ()

Cases:

>>>forin enumerate(s):>>>    print i,t>>>s,t=‘foa‘,‘obr‘>>>zip(s,t)[(‘f‘,‘o‘),(‘o‘,‘b‘),(‘a‘,‘r‘)]

(3) String type function

    • Raw_input ()
    • STR () and Unicode ()
    • Chr ()/UNICHR ()
    • Ord () (Ord () is a pairing function of Chr () or UNICHR ()

There is no C-style end character nul in Python.

6. String built-in function

  • 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 in a string and returns the number of STR occurrences within a specified range if beg or end specifies
  • String.decode (encoding= ' UTF-8 ', errors= ' strict ') decodes a string in encoding specified encoding format, and if an error defaults to a ValueError exception, Unless errors specifies ' ignore ' or ' replace '
  • String.encode (encoding= ' UTF-8 ', errors= ' strict ') encodes a string in encoding specified encoding format, and if an error defaults to a ValueError exception, Unless errors specifies ' ignore ' or ' replace '
  • String.endswith (Obj,beg=0,end=len (string)) checks whether the string ends with obj, and returns False if beg or end specifies whether the specified range ends with obj, or if True is returned.
  • String.expandtabs (tabsize=8) Turns the tab symbol in string strings to a space, with the default number of spaces Tabsize 8
  • String.find (Str,beg=0,end=len (string)) checks if STR is contained in a string, and if beg and end specify a range, the check is contained within the specified range, or returns 1 if the index value is returned.
  • String.index (String) is the same as the Find () method, except that STR does not report an exception in the 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.isdecimal () returns True if string contains only decimal digits, 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 (case-sensitive) 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
  • String.istitle () returns True if string is a caption, otherwise false
  • String.isupper () returns True if the string contains at least one case-sensitive character, and all of these (case-sensitive) characters are uppercase, otherwise false
  • String.Join (seq) merges all elements in the SEQ (string representations) into a new string as a delimiter of string
  • String.ljust (width) returns the left alignment of an original string and fills the new string with a space of length width
  • String.Lower () converts all uppercase characters in a string to lowercase
  • String.lstrip () truncates the left space of a string
  • String.partition (str) is a bit like the combination of find () and split (), which separates string strings into a 3-tuple (STRING_PRE_STR,STR,STRING_POST_STR) from the first position where STR appears. If STR is not included in the string, the string_pre_str==string
  • String.Replace (Str1,str2,num=string.count (STR1)) replaces str1 in string with STR2, and if NUM is specified, it is replaced by no more than NUM times
  • String.rfind (Str,beg=0,end=len (String)) is similar to the Find () function, but looks up from the right.
  • String.rindex (Str,beg=0,end=len (String)) is similar to the index () function, but looks from the right.
  • String.rjust (width) returns the right alignment of the original string and fills the new string with a space of length width
  • String.rpartition (str) is similar to the partition () function, but looks from the right.
  • String.rstrip () Remove space at the end of a string string
  • String.Split (str= "", Num=string.count (str)) slices a string with the Str delimiter, and if NUM has a specified value, only the NUM substring is delimited
  • String.splitlines (Num=string.count (' \ n ')) returns a list containing rows as elements, separated by rows, if NUM specifies that only num rows are sliced
  • String.startswith (Obj,beg=0,end=len (string)) checks whether the string begins with Obj, and returns False if beg or end specifies whether to start with obj in the specified range, or if it returns true
  • String.strip () executes the Lstrip () and Rstrip () functions on a string
  • String.swapcase () flips the casing in a string
  • String.title () returns a string that is "heading", meaning that all words start with uppercase and the remaining letters are lowercase
  • String.translate (str,del= "") converts string characters according to the table given by STR (contains 256 characters), and the characters to be filtered out into the del parameter
  • String.upper () converts all lowercase characters in a string to uppercase
  • String.zfill (width) returns a string of length width, the original string is right-aligned, the front padding 0

7. Unique characteristics of strings
(1) Special strings and control characters
Special characters (commonly used as delimiters within a string)
Backslash + single character
ASCII code value Representation
Nul (\000) characters can appear more than once in a python string (terminator in the C language)
Escape character beginning with backslash

    • NUL NULL character NUL
    • \a BEL Bell character
    • \b BS Backspace
    • \ t HT transverse tab
    • \ nthe LF.
    • \v VT Portrait tab
    • \f FF Replacement Page
    • \ r CR Return
    • \e ESC Escape
    • \ "" Double quotation marks
    • \ ' Single quotation mark
    • \ \ Backslash

(2) Three quotation marks ("' strcontent '")
Strings enclosed in three quotes allow strings to span multiple lines, and strings can contain line breaks, tabs, and other special symbols.
The syntax for a triple quotation mark is a pair of consecutive single or double quotes (usually paired).
(3) String invariance

>>>s=‘abc‘>>>s=s+‘def‘

When the "S+ ' def" Operation is made, a new string is created, and the new string object is assigned to S, and the original ' ABC ' is freed.

8, Unicode (1.6 introduction of Unicode string support)
(1) Related terms

    • ASCII American Standard Information Interchange code
    • BMP Basic Multilingual Plane (0th plane)
    • BOM byte order mark (character that identifies byte order)
    • CJK/CJKV Chinese-Japanese-Korean (and Vietnamese) abbreviations
    • Code point is similar to an ASCII value that represents the value of a Unicode character, in range (1114112) or from 0x000000 to 0X10FFFF
    • Octet bit groups of eight-bit binary numbers
    • UCS Universal Character Set
    • UCS2 UCS Double-byte encoding method
    • UCS4 UCS Four-byte encoding method
    • The conversion format for UTF Unicode or UCS
    • UTF-8 8-bit UTF conversion format (unsigned byte sequence with a length of 1~4 bytes)
    • UTF-16 16-bit UTF conversion format (unsigned byte sequence, usually 16 bits long [two bytes], see UCS2)

(2) How Unicode is used in Python
All string literals in Python are ASCII-encoded by default, and Unicode strings can be declared using the ' u ' prefix.
The string module has stopped updating with only the ASCII code support, and the string module is deprecated.
Chr ()/str () can handle only regular ASCII-encoded strings.
The Unicode ()/UNICHR is a Unicode version of STR ()/CHR ().
(3) What is codec?
Codec is the Coder/decoder first letter combination. Defines the way in which text and binary values are converted. Unicode is multi-byte encoding. Codec supports 4 encoding methods: Ascii/iso 8859-1/latin-1/utf-8/utf-16. UTF-8 uses 1-4 bytes to represent characters. Encodes ASCII characters using a single byte. UTF-16 uses two bytes to store characters, and UTF-16 is not backwards compatible with ASCII.
(4) Note when processing Unicode strings

    • When a string appears in the program, you must prefix ' u ';
    • Use the str () function instead of the Unicode () function;
    • No outdated string modules;
    • Unicode characters are not encoded in code until they are required.

Most modules in Python support Unicode, while the Pickle module supports only ASCII strings. But now the default format for the Pickle module is binary format, so it is much better to store the file as a BLOB field than to store it as a text or varchar field when it comes to the data inventory file.
(5) Unicode support for Python

    • Built-in Unicode () functions
    • Both the built-in decode ()/encode () methods can be applied to regular strings and Unicode strings.
    • Unicode type: You can create a Unicode string instance by using the Unicode () factory method or by adding a U or u in front of the string.
    • The Ord () function has been upgraded to support Unicode objects;
    • The Unichar () function takes a 32-bit value to return a corresponding Unicode character.
    • A mixed-type string operation requires the conversion of a normal string into a Unicode object.
    • Unicodeerror exception.
    • The regular expression engine requires Unicode support.
    • String formatting operator: U "%s"% (U "abc", "abc") = + u "abc abc"

9. String-related modules in the Python standard library

    • String string manipulation of related functions and tools, such as the template class
    • Re Regular expression: powerful string pattern matching module
    • Conversion between struct string and binary
    • C/stringio A string buffer object, similar to a file object
    • Base64 Base 16, 32, and 64 data codec
    • Codecs decoder registration and base class
    • Crypt for unilateral encryption
    • Difflib (2.1) Find out the difference between sequences
    • Hashlib (2.5) API for multiple different secure hashing algorithms and information digest algorithms
    • Implementation of the data authentication algorithm for HMA (2.2) HMAC using Python
    • MD5 RSA's MD5 Information Digest authentication
    • Rotor provides multi-platform encryption and decryption services
    • Sha Secure Hash Algorithm Sha
    • Stringprep (2.3) provides a Unicode string for the IP protocol
    • TextWrap (2.3) text wrapping and padding
    • Unicodedata Unicode Database

10. List
The list can hold any number of Python objects.
Tuples are immutable (read-only).
The list is defined by square brackets ([]).
Use the slice operator ([]) and the index to access the list elements.
List elements can be updated. You can also use the Append () method to add elements.
Delete list element with Del statement del Alist[1]
The Remove () method can be used to delete an element Alist.remove (123)
You can use the Pop () method to delete and return a specific object from the list.
Delete List object with Del del alist

11. Operator
(1) standard type operator
> < = etc
(2) Sequence type operator

    • Slice ([])
    • Member relationship action (In,not in)
    • The Join operator (+) can only be performed between objects of the same type.
      From 1.5 You can use the Extend () method instead of the join operator, which has the advantage of adding a new list to the original list without creating a new list. The List.extend () method is also used to perform compound assignment operations, which is the replacement join operation (+ =) added in 2.0.
    • Repeating operator (*)
    • List parsing [i*2 for i in [8,-2,5]] =>[16,-4,10]

12. Built-in function
(1) standard type function
CMP () List comparison
(2) Sequence type function

    • Len ()
    • Max ()/min ()
    • Sorted ()/reversed ()
    • Enumerate ()/zip ()
    • SUM ()

List ()/tuple () accepts an iterative object and generates a new list or tuple by shallow copying the data. They are often used for the type conversions of lists and tuples.

13. Built-in functions for list types
Dir (list) uses the Dir () method to get all the methods and properties of the list object.

    • List.append (obj)
    • List.count (obj)
    • List.extend (seq)
    • List.index (Obj,i=0,j=len (list))
    • List.insert (Index,obj)
    • List.pop (Index=-1)
    • List.remove (obj)
    • List.reverse ()
    • List.sort (func=none,key=none,reverse=false) sorts the elements in the list in the specified manner, and if the Func and key parameters are specified, the individual elements are compared in the specified manner, and if the reverse flag is set to True, The list is sorted in reverse order

Note: There is no return value for those mutable objects that can change the value of an object.

14, meta-group
Tuples with parentheses. Tuples are immutable types.
A tuple can be used as a dictionary key. This group is the tuple type by default when working with a set of objects.
Creating tuples with only one element requires a comma (,) in the tuple delimiter to prevent confusion with normal grouping operators. >>>t= (' abc ',)
Delete the tuple with the DEL statement.
The tuple operator and the built-in function are the same as the list.

15, the special characteristics of the tuple
The tuple is immutable. However, the join operation connects several small tuples into a single large tuple.
The repeating action also applies to tuples. Tuples can be converted into lists.
The tuple object itself is immutable, but the tuple contains mutable objects that can be modified.
All multi-object, comma-delimited, and not explicitly defined with symbols (square brackets, parentheses), the default types of these collections are tuples.
The multiple objects returned by all functions (not including the signed package) are tuple types.
Create a single element tuple (' xy ') because the parentheses are overloaded as a grouping operator, the parentheses are first used as a grouping operation, and then as a delimiter for tuples.
An immutable type is required when the function is called to ensure that the incoming parameter is not tampered with.
When you need a mutable type, when you manage a Dynamic data collection, you need to create them, add them gradually or irregularly, or sometimes you need to remove some individual elements.
Array types are similar to list types, except that all of their elements are required to be of the same type.

16. Related Modules

    • Array arrays
    • Copy deep copy and shallow copy
    • Operator a sequence operator that contains the form of a function call
    • Re Perl-style regular expression lookup (and matching)
    • Stringio long string as file operation, read ()/seek (), etc.
    • Cstringio long string as file operation (version C, faster)
    • TextWrap (2.3) function to wrap/fill text
    • Types includes all types supported by Python
    • Collections (2.4) High performance container data types

17. Copying Python objects
Object assignment is simply a simple object reference.
A shallow copy, an immutable element is explicitly copied, and a mutable element simply copies the reference.
A shallow copy of a sequence type object is implemented in the following ways:

    • Full slice operation ([:])
    • Factory function List ()/dict (), etc.
    • Copy () function of the copy module

Deep copies are implemented using the Copy.deepcopy () function.
A few caveats about copy operations:

    • Non-container types (such as numbers, strings, and other "atomic" types of objects, such as code, types, and Xrange objects, etc.) are not copied, and a shallow copy is done with a full slice operation.
    • If a tuple variable contains only an atomic type object, a deep copy of it does not take place.

Python Learning note 6:python sequence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.