This article describes the Python list comprehension operations in examples. Share with you for your reference, as follows:
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
[ 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
[ x for x inrange(100)if x%2==0]
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]]
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
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()……
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']
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