Sequence in Python

Source: Internet
Author: User

Sequence in Python

 

Python has four built-in data structures: List, tuples, dictionaries, and collections. The dictionary and set will be written later. Now let's talk about the list and meta-group. They are two strings that have been mentioned many times,

Actually all belong to the -- sequence.

I. list ):

1. list is the data structure that processes a group of ordered projects. You can store a series project in a list.

The items in the list should be included in square brackets so that Python knows that you are specifying a list. Once you create a list, you can add, delete, or search for projects in the list. Because you can

To add or delete a project, we say that the list is a variable data type, that is, this type can be changed.

The list can be divided into common (same data type), hybrid (mixed data type), and empty lists based on the elements in the list.

List some frequently used built-in Methods: append (), extend (), insert (), remove (), del, pop (), count (), index (), reverse (), sort (), and so on. Parameters and

The usage is very simple. You only need to describe one. sort function: sort.([*, Key = None, reverse = None]), Note that in Python3, the first parameter is immutable (the Sorting Algorithm is sorted by merge by default) and other real parameters cannot be input,The second parameter indicates the keyword that participates in sorting. You can use the lambda function:

>>> L = [('B', 2), ('A', 1), ('C', 3), ('D', 4)]
>>> L. sort (key = lambda x: x [1])
>>> L
>>> [('A', 1), ('B', 2), ('C', 3), ('D', 4)] the third parameter reverse has two optional values. The default value "False" indicates the sorting from small to large, and the value "True" indicates the sorting from large to small. All three parameters are optional. 2. many operators can also be used for lists, such as comparison operators, logical operators (and or not), and '+' Concatenation Operators (can only be used for Splicing between lists and cannot be used to add elements equivalent to extend ), '*' repeat operator, membership operator (in/not in ). Pay attention to the usage and operation rules of operators. 3. The list can also be operated using slice. The usage rules are almost identical to those of strings. 4. note that y = x [:] copies all the elements of list x to y through the slicing operation. If you simply assign x to y: y = x, y and x still point to the same list, and no new copies are generated. Note: The variable name in Python is actually equivalent to a tag. Ii. tuple ):1. tuples are used to aggregate various objects. The tuples are similar to the list, except that the tuples and strings are immutable, that is, you cannot modify the tuples. The tuples are defined by commas (,) in parentheses. Tuples are usually used to securely use a group of values for statements or user-defined functions. That is, the values of the used tuples do not change. 2. Note that the most important thing in tuples is not parentheses, but the ',' sign. In particular, if Python only has one element, it must be written as (a,). Remember the following two examples: a = (3), type (a) = int; B = 3, 4, 5 type (B) = tuple. We can see that the comma in the tuples is the key, or even the parentheses are not required. 3. The operators that can act on the list mentioned above can also act on tuples. 4. because tuples are immutable, if you really need to "change" the elements of the tuples, you can consider the splicing method, such as >>> L = ('I ', 'Love', 'you') >>> L = L [: 1] + ('really ',) + L [1:] >>> L ('I', 'Really ', 'love', 'you') Note that the essence here is not "changing" tuples, And the tuples themselves are immutable. The essence here is to construct a new tuples through splicing, And Then paste the 'l' label to the new tuples. The old tuples still exist and remain unchanged. (Of course, Python's garbage collection mechanism will recycle it at an appropriate time) 3. string ):1. Here we repeat the string mainly as a sequence to learn about some of its wonderful built-in methods. 2. the built-in methods of the two most commonly used strings that must be mentioned here: (1). join (sub). Use a string as the separator to insert all the characters in the sub string parameter. (2). format (): It is equivalent to formatting output in C. There are two types of parameters: Location Parameter and keyword parameter. The remaining formatting symbols are similar to those in the C language. For example, Note: There are many formatting outputs, but not many are actually used. Iv. Sequence (serial ):1. A sequence is a string, A tuples, and a list collectively. Sequences have the following features: --- you can get each element through the index --- the default index value always starts from scratch --- you can get a set of elements in a range through the slicing method --- there are many common operators (repeated operators, concatenation operator and member relationship operator) 2. python has many built-in functions for sequences: list (), tuple (), str (), len (), max (), min (), sum (), sorted (), reversed (), enumerate (), zip (), and so on. Note: In Python, sorted is a built-in function (BIF), while sort () is a built-in method (function) list. sort () of the list type (). The former can act on any type of sequence, or even dictionary sorting. The latter can only sort the list type.

 

Related Article

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.