Advanced Actions for lists

Source: Internet
Author: User

1 What is a list list?

A list is a combination of data types in Python

What is a list

You need to unify the storage of multiple repeatable data in a single variable

The role of the list

Multiple data can be managed uniformly

How to operate the list?

Declaration of a list

a = list()  a = []  
Data processing for lists

Delete and change "traverse"
Add one data to the insert specified position
Append a data to the end of append
Extend Stitching two lists
Pop if no subscript is specified, the last element is deleted by default
Remove specifies that a data is deleted
Clear clears all data
Index list data
Del deletes data based on index
Reverse list Inversion
Sort sorts the list according to the ASCII code

List of Use defects

The data is stored directly in the list, but if there is more data in the list, it may consume more memory "list data items, more memory consumption"

2 simple derivation of a list deduction formula

my_list = [x for x in range(0, 1000)]
Generate a list of 0~999 natural number sequences

Arithmetic derivation formula

my_list = [x**2 for x in range(0, 100)]
Generates a list containing the 0~99 squared sequence

Conditional Deduction Formula

my_list = [x for x in range(0, 100) if x % 2 == 0]

Combination derivation formula

my_list = [x + y for x in range(0, 5) for y in range(0, 10)]
Generate a list of 0~4, and 0~9 two-dimensional data permutations and additions

3 List Builder

Generator syntax structure and derivation syntax structure and similar
A basic generator: Generate data for a sequence of 0~10 natural numbers
my_generator = (x for x in range(0, 10))
Direct presentation is a generator object
The generator object must pass the system Mole function next () to get the next data for the generator
The excellent performance of the generator is that the next data will be calculated at the time of use, without loading the data all at once

4 Loop Traversal and iterators

In the program, we often encounter such objects, through the for loop can be directly loop iteration
A tool object is provided in Python: an iterator objectcollections.Iterable
The function of the object __iter__() can be an iterative object:collections.Iterator
List is only a type, so it is neither an iterator object nor an iterative object
A specific list object is an iterator object that can be manipulated by a for loop that produces an iterative object that indexes the data of the iteration

5 differences between generators and derivations
    • Derivation: Use a tear-off before running, get a complete list; When the generator uses the list to run, it gets the next list element
    • The builder is to change the inferred syntax square brackets to parentheses
    • The derivation generates a list, which contains the concrete element data, can use index subscript to obtain the corresponding data data, the operation consumes more time, consumes more memory; The generator generates an object [generator] that contains the arithmetic rules next()/__next__() to get the data, regardless of the size of the data
6 The difference between a generator and an iterator

The generator primarily defines a data generation rule that obtains a special object like list data through a run-time algorithm
Iterators are a special object used to iterate through iterative data, including iterative objects and iterative processes
Contact: The generator object is an iterative object that can be traversed by iterators.

Advanced Actions for lists

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.