Python Composite data types

Source: Internet
Author: User
Tags iterable
sequence Type

Python provides five types of built-in series:
ByteArray
bytes
List
Str
Tuple META Group

A tuple is an ordered sequence that contains 0 or more object references.
Like a string, a tuple is also fixed, so you cannot replace or delete any of its data items. If you need to modify it, we should use a list instead of a tuple, and if we have a tuple but we have to modify it, you can use the list conversion function to convert it to a list and then make the appropriate changes to the list.
Tuple ()-Returns an empty tuple when no parameters are specified. The function can accept at most one parameter.
Tuples provide only two methods:
T.count (x) returns the number of times an object x appears in a tuple;
T.index (x), which returns the leftmost position of the object in the tuple T-when the tuple does not contain x, produces a ValueError exception (which is also available for the list). named tuples

A named tuple, like an ordinary tuple, has the same performance feature that it adds the ability to refer to an item in a tuple based on its name, which, as per the index position, allows us to create aggregation of data items.
The Collections module provides the Namedtuple () function, which is used to create a custom tuple data type, for example:
person = collections.namedtuple ("person", ' name Sex age ')
The first parameter of Collections.namedtuple () is the name of the custom tuple data type that you want to create, and the second parameter is a string that contains a space-delimited name that represents an entry for the tuple data type. List

A list is an ordered sequence that contains 0 or more object references.
Unlike strings and tuples, the list is mutable, so it is possible to delete and replace items in the list, insert, replace, or delete the fragments in the list.
The list data type can be invoked as a function, list ()-when electrophoresis without parameters, an empty list list method is returned

at the index position int i
Syntax description
l.append (x) Append data item x to the end of list L
l.count (x) Returns the number of data item x occurrences in L
l.extend (m) or L + = m appends the item iterable m to the end of L, and the operator = completes the same function
L.index (x,start,end) returns the leftmost index position of the data item X in list L (or [start:end] fragment of L), otherwise a ValueError exception is generated
L.insert (i,x) Inserts a data item X into the list L
l.pop () returns and removes the rightmost data item from list L
L.pop (i) returns and removes the data item at index position int i in L,
l.remove (i) removes the leftmost data item X from list L, and generates VALUEERROR exceptions if no x is found
l.reverse () reverses list L
l.sort (...) sort list L, as with the built-in sorted () function, which accepts optional key and reverse parameters

Although you can use the fragment operator to access data items in a list, in some cases we need to extract two or more data items that can be implemented using sequence splitting. Any iteration (list, tuple, etc.) data type can be split using the sequence split operator, that is, *. For the two or more variables to the left of the assignment operator, one uses the * boot, the data item is assigned to the variable, and all the remaining data items are assigned to the variable with the asterisk, and here are some examples:
*rest = [1,2,3,4,5] \ #fisrt = 1, rest = [2,3,4,5], *mid, last = [1,2,3,4,5] \ #first = 1, mid = [2,3,4], Las t = 5 list connotation

[Expression for item in interable]
[Expression for item in Interable if condition]
For the second syntax, it is actually equivalent to:
temp = [] for item in Iterable:if condition:temp.append (expression)
If the generated list is very large, generating each item as needed is more efficient than generating the entire list at once, which can be implemented through the builder instead of the list connotation. Collection

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.