In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what are the analytical expressions of Python". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Python analytic formula
You can often see assignment statements like ret = [x * * 2 for x in lst] in python, but it is not easy for people who transfer from C++ to python to understand the use of this for loop. This is the new syntax invented by python for brevity. The python parser has the following advantages:
The code is concise and readable.
The efficiency is slightly higher than that of ordinary iteration.
The analytical expressions of python can be divided into the following four categories:
List analytic expression
Generator analytic expression
Set analytic formula
Dictionary analytic formula
The use of these four analytical expressions is described below.
List analytic expression
The form of list analytic expression
[expr for e in iterator]
In [1]: lst = range (10) In [2]:% timeit.: ret = [x * * 2 for x in lst]...: 100000 loops, best of 3: 5.28 μ s per loopIn [3]:% timeit...: ret = []...: for x in lst:...: ret.append (x * * 2)...: 100000 loops, best of 3: 6.09 μ s per loop # takes a little more time
You can find that the efficiency is a little higher, the most important thing is the simplicity of the code.
List parsers can be used with if statements
For example, filter out the even numbers in the list lst:
In [4]: ret = [] In [5]: for x in lst:...: if x% 2 = = 0:...: ret.append (x) # use the for loop.: In [6]: retOut [6]: [0,2,4,6 8] In [7]: ret = [x for x in lst if x% 2 = = 0] # use the list analytic expression In [8]: retOut [8]: [0,2,4,6,8]
List parsers can use if statements like for loops.
With multiple if statements, can be transformed into conditional logic operations, so generally speaking, there will not be multiple if statements
For statements of list parsers can be nested.
In [9]: (x, y) for x in range (0,5) for y in range (5,10) File ", line 1 (x, y) for x in range (0,5) for y in range (5,10) ^ SyntaxError: invalid syntax# description list must be enclosed in parentheses In [10]: [(x, y) for x in range (5) for y in range (5,10)] Out [10]: [(0) 5), (0,6), (0,7), (0,8), (0,9), (1,5), (1,6), (1,7), (1,8), (1,9), (2,5), (2,6), (2,7), (2,8), (2,9), (3,5), (3,6), (3,7), (3,8) (3,9), (4,5), (4,6), (4,7), (4,8), (4,9)] In [11]: ret = [] In [12]: for x in range (5):...: for y in range (5,10):...: ret.append ((x, y)).: In [13]: retOut [13]: [(0,5)] (0,6), (0,7), (0,8), (0,9), (1,5), (1,6), (1,7), (1,8), (1,9), (2,5), (2,6), (2,7), (2,8), (2,9), (3,5), (3,6), (3,7), (3,8), (3) 9), (4,5), (4,6), (4,7), (4,8), (4,9)]
Special usage of if statement
A single-line if statement is written much like a list parser.
Expression form: x if cond else y
If and else must exist together.
Take the even number square and the odd number cube as examples to demonstrate.
In [14]: ret = [] In [15]: for x in lst:...: if x% 2 = = 0:...: ret.append (x * * 2)...: else:...: ret.append (x * * 3)...: In [16]: retOut [16]: [0,1,4,27,16,125,36,343,64 In [17]: X = if special usage In [18]: X * * 2 if x% 2 = = 0 else x * * 3Out [18]: 27In [19]: 3 if True else 4Out [19]: "if if special usage is used with the list analytic expression x if cond else y for. In [20]: [x * 2 if x% 2 = 0 else x * * 3 for x in lst] Out [20]: [0,1,4,27,16,125,36,343 64, 729] generator analytic expression
The list parser returns a list, while the generator parser returns a parser. The parenthesis of the list parser becomes parenthesis of the generator.
In [1]: range (10000) Out [1]: range (0, 10000) In [2]: G = (x * * 2 for x in range (100000000000)) In [3]: gOut [3]: In [4]: next (g) Out [4]: 0In [5]: next (g) Out [5]: 1In [6]: next (g) Out [6]: 4
Selection of list parser and generator parser
When you need to use subscript to access, use list parsing
When you only need to iterate over the results, the generator parser is preferred.
Set analytic formula
Replacing the square braces of a list parser with curly braces is a collection parser.
In [1]: lst = [2, 4, 5, 6, 3, 4, 2] In [2]: s = {x for x in lst} In [3]: sOut [3]: {2, 3, 4, 5, 6} # it can be seen that when the list analytic expression is generated, the repetition is removed, which meets the set requirements In [4]: type (s) Out [4]: set dictionary analytical formula.
Dictionary parsers also use curly braces, but unlike collection parsers, instead of individual elements are used at expr, kPowerv pairs are used.
In [1]: {str (x): x for x in range (5)} Out [1]: {'0: 0,'1: 1,'2: 2,'3: 3,'4}
The most widely used of the four parsers is the list parser, which is often used in some clever ways.
This is the end of the content of "what are the Python parsers"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.