In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the python use list derivation of the example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
Use list derivation
A list derivation consists of the following sections:
An input sequence
A variable that represents the members of the input sequence
An optional assertion expression
An output expression that transforms a member of an input sequence that satisfies an assertion expression into an output list member
Num = [1, 4,-5, 10,-7, 2, 3,-1] filtered_and_squared = [] for number in num: if number > 0: filtered_and_squared.append (number * * 2) print filtered_and_squared# [1,16,100,4,9]
If you use the filter, lambda, and map functions, you can greatly simplify the code:
Num = [1, 4,-5, 10,-7, 2, 3,-1] filtered_and_squared = map (lambda x: X * * 2, filter (lambda x: X > 0, num)) print filtered_and_squared# [1,16,100,4,9] # A simplified way to write num = [1,4,-5,10,-7,2,3 -1] filtered_and_squared = [xylene 2 for x in num if x > 0] print filtered_and_squared# [1,16,100,4,9]
List derivation may also have some negative effects, that is, the entire list must be loaded in memory at once, which is not a problem for the example above, even after it has been expanded several times. But it will always reach the limit and the memory will always be used up.
In view of the above problems, the generator (Generator) can solve very well. Instead of loading the entire list into memory at a time, the generator expression generates a generator object (Generator objector), so only one list element is loaded at a time.
The generator expression has almost the same syntax structure as the list derivation, except that the generator expression is surrounded by parentheses instead of square brackets:
Num = [1, 4,-5, 10,-7, 2, 3,-1] filtered_and_squared = (xylene 2 for x in num if x > 0) print filtered_and_squared# for item in filtered_and_squared: print item# 1,16,100 4jue 9
This is slightly more efficient than list derivation, so let's modify the code again:
Num = [1, 4,-5, 10,-7, 2, 3,-1] def square_generator (optional_parameter): return (x * * 2 for x in num if x > optional_parameter) print square_generator (0) # # Option Ifor k in square_generator (0): print k # 1,16,100,4, Option IIg = list (square_generator (0)) print g # [1,16,100,4,9]
Unless for special reasons, you should often use generator expressions in your code. But unless you are faced with a very large list, you will not see the obvious difference. Let's take a look at an example of traversing a directory through a two-order list:
Import osdef tree (top): for path, names, fnames in os.walk (top): for fname in fnames: yield os.path.join (path, fname) for name in tree ('C:\ Users\ XXX\ Downloads\ Test'): print name thanks you for reading this article carefully. I hope the article "sample Analysis of python use list derivation" shared by the editor will be helpful to you. At the same time, I hope you will support it. Pay attention to the industry information channel, more related knowledge is waiting for you to learn!
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.