Python list comprehension operation example summary

This article describes the Python list comprehension operations in examples. Share with you for your reference, as follows:

List comprehension###

One of Python's powerful features is its parsing of lists, which provides a compact way to map one list to another list by applying a function to each element in the list.
List comprehension, also called list comprehension (list comprehension)
List comprehensions are more streamlined and faster than for, especially for larger data sets
List comprehensions can replace most situations where map and filter are needed

List comprehensions provide a simple way to create linked lists without using map(), filter(), and lambda. It is usually clearer to get lists in a defined way than to create them with a constructor. Each list comprehension includes an expression after a for statement, and zero or more for or if statements. The return value is a list of elements obtained from the expression after the for or if clause. If you want to get a tuple, you must add parentheses.

Basic list comprehension#####

Basic

[ x for x inrange(5)]  # [0,1,2,3,4]
l1 =[1,2,3,4][ x*2for x in l1]  #[2,4,6,8]

**Multiple values **

[' %s = %s'for(k, v)in a_map.items()]

Two cycles

 l1 =[1,2,3,4]
 l2 =[1,2,3,4][x+y for x in l1 for y in l2][2,3,4,5,3,4,5,6,4,5,6,7,5,6,7,8]

Function can be called

[ func(x)for x in l1] #Equivalent to map

Note that the list comprehension will not change the value of the original list, it will create a new list

Conditional list analysis
[ x for x inrange(100)if x%2==0]
Nested list comprehension#####
mat =[[1,2,3],[4,5,6],[7,8,9]]

Exchange ranks

[[ row[i]for row in mat]for i in(0,1,2)] #[[1,4,7],[2,5,8],[3,6,9]]
Other:
  1. When fetching elements according to the index, boundary checking is required. IndexError slice fetching, no need, it will not be abnormal if the boundary is exceeded

  2. Modify the list in the iteration. Note that it is not safe. It is not recommended to do this, but you can for i in l1[:]: l1.insert()……

  3. Multiple lists into one is

[' a','b',.....],['a','b'.....]['a','b'.....]

Becomes

[' a','b',.....,'a','b'.....'a','b'.....]

deal with

sum([['a','b'],['a','b'],['a','b']],[])['a','b','a','b','a','b']list(itertools .chain(['a','b'],['a','b'],['a','b']))['a','b','a','b','a','b']
  1. About stacks and queues

Through the above operations, you can find that you can easily use the list as a stack or queue
Of course, they have their own modules, you can check related libraries

5. Sequence related modules

array A restricted variable sequence type, requiring all elements to be of the same type

copy provides shallow copy and deep copy capabilities

operator contains sequence operators in the form of function calls, such as operator.concat(m,n) is equivalent to m+n

re regular expression

types contains all types supported by Python

collections high-performance container data types

For more information about Python related content, please refer to the topic of this site: "Python list (list) operation skills summary", "Python string operation skills summary", "Python data structure and algorithm tutorial", "Python function usage skills summary", " Python entry and advanced classic tutorial" and "Python file and directory operation skills summary"

I hope this article will help you in Python programming.

Recommended Posts

Python list comprehension operation example summary
Python negative modulus operation example
What is list comprehension in python
Python3 list
Python decorator simple usage example summary
Example operation of python access Alipay
Python file and directory operation code summary
Python file operation
Python object-oriented example
Python basic summary
python list learning
python operation kafka
[902] python list sort
Example of feature extraction operation implemented in Python
Python3.7 debugging example method
Python processing json summary
Python interview questions summary
Python operation SQLite database
Python advanced usage summary
Python operation yaml instructions
Python interpolate interpolation example
Python-openCV open operation example
Python automated operation and maintenance 2
Python operation Excel merge cells
Python high-order function usage summary!
In-depth understanding of python list (LIST)
Python datetime processing time summary
Python basic syntax list production
Python3 logging log package example
Python regular expression example code
Python output mathematical symbols example
Python automated operation and maintenance 1
Python iterable object de-duplication example
LeetCode brushing questions summary python3
Python one-dimensional two-dimensional interpolation example
Quick start Python file operation
Python draw bar graph (bar graph) example
Python right alignment example method
Python high-order function usage summary!