Python sequence type, python Sequence

Source: Internet
Author: User
Tags python list

Python sequence type, python Sequence

In python,Sequence typeIncludingString(Common string and unicode string ),ListAndTuplesThe so-called sequence, that is, the sequential arrangement of members, can be accessed by subscript. Assume that there are n elements in the sequence. when accessing a single element, the subscript starts from 0 and ends with n-1. If it is in reverse order, the subscript starts from-1 and ends with-n, slice operations support access to multiple elements.

Sequence Types support the following operators --

In/not in: Member relationship Operator

Obj [not] in sequence: determines whether (not) The obj element belongs to the sequence.

+: Join Operator

Sequence1 + sequence2: connects two sequences of the same type.

*: Repeated Operator

Sequence * copies_int: The sequence Repeats copies_int times.

[],[:],[:]: Slice Operator

Sequence [index]: access a single element.

Sequence [starting_index: ending_index]: access to the elements at the beginning, end, and bottom labels. None is supported as the index value, which is equivalent to not specifying this index value.

Sequence [starting_index: ending_index: step]: access the elements at the beginning, end, and bottom labels. The step length between specified elements supports None as the index value, which is equivalent to not specifying this index value.


Sequence types can be used as the following built-in functions --

List (iter): Converts an iteratable object to a list.

Str (obj): Convert the obj object to a string, that is, the object is represented by a string.

Tuple (iter): Converts an iteratable object into a tuples.

Unicode (obj): Converts an object to a Unicode string.

Basestring (): Abstract Factory functions only provide parent classes for str and unicode functions, so they cannot be instantiated or called.

Enumerate (iter): Accepts an iteratable object as a parameter and returns an enumerate object, which generates a tuple consisting of the index value and item value of each element of iter.

Len (seq): Return the length of seq.

Max (iter, key = None),Max (arg0, arg1...., key = None): Return the maximum value of iter or (arg0, arg1. ..). If a key is specified, the key must be a callback function that can be passed to the sort () method for comparison.

Min (iter, key = None),Min (arg0, arg1...., key = None): Return the minimum value of iter or (arg0, arg1. ..). If a key is specified, the key must be a callback function that can be passed to the sort () method for comparison.

Reversed (seq): Receives a sequence as a parameter and returns an iterator accessed in reverse order.

Sorted (iter, cmp = None, key = None, reverse = False): Accepts an iteratable object as a parameter and returns an ordered list. The optional parameters cmp, key, reverse, and list. sort () have the same built-in function meaning.

Sum (seq, init = 0): Returns the sum of seq and the optional parameter init. The result is equivalent to reduce (operator. add, seq, init ).

Zip ([it0, it1. ..]): Returns a list. The first element of the list is a tuple consisting of the first elements of it0, it1.


PythonString --

The string type is the most common type in python and is an unchangeable type. It supports single quotes, double quotes, and three quotes. The three quotes are a pair of continuous single quotes or double quotes, allowing a string to span multiple rows.

String connection: The + operator mentioned above can be used for string connection, and several strings can be directly connected and written together. In addition, the join () method can be called to connect strings.

Only applicable to operators connected by strings: We have mentioned some operators shared by sequence types. In addition, strings also have their own operators, including the format control operator % and string template string. template, original string operator r/R, and Unicode string operator u/U.


The following lists some modules related to the string type.

String: Functions and tools related to string operations, such as the Template class.

Re: Regular Expression, powerful string mode matching module.

Struct: Switch between the string and binary.

C/StringIO: String Buffer object. The operation method is similar to file object.

Base64: Base16, 32, and 64 data encoding/decoding.

Codecs: Decoder registration and base class.

Crypt: Perform unilateral encryption.

Difflib: Find out the differences between sequences.

Hashlib: Python2.5 abolished APIs for different security algorithms and information digest algorithms.

Hma: Python implementation of the hmac information authentication algorithm.

Md5: RSA digest authentication.

Rotor: Provides multi-platform encryption and decryption services.

Sha: The security algorithm SHA of NIAT.

Stringprep: Provides Unicode strings for the IP protocol.

Textwrap: Text packaging and filling.

Unicodedata: Unicode database.


PythonList --

The python list is represented by square brackets [] and has the container and variable features, which makes it very flexible. It is not difficult to build other data structures, such as stacks or queues.


PythonTuples --

The python tuples are represented by parentheses (), which is another container type very similar to the list. The difference is that the tuples are of an unchangeable type. The default type of all multi-object sets that are separated by commas (,) and do not explicitly define with symbols is tuples.


The following lists some modules related to sequence types.

Array: A limited variable sequence type. All elements must be of the same type.

Copy: Provides the shortest copy and deep copy capabilities. Deep copy deepcopy () is only valid for variable types.

Operator: Contains sequence operators in the form of function calls.

Types: Contains all types supported by python.

Collections: High-Performance container data type.


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.