In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The main content of this article is to explain "Python full stack push and generator how to achieve", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Python full stack push and generator how to achieve" it!
1. Deductive # deductive: the method of traversing some columns of data through an one-line loop is called deductive syntax: val for val in iterable. Deductive basic grammar lst = [] for i in range (1Mague 51): lst.append (I) print (lst) # rewrite deductive lst = [i for i in range (1MJ 51)] print (lst) # Mini exercise # 1. [1Min 2 for i in range 3 Min 4 Jing 5] = > [2 Min 4 Min 6 8J 10] lst = [ife2 for i in range (1 min 6)] print (lst) # 2. Inference with judgment condition Note: the judgment condition closely followed by for can only be a single branch. [1Med 2, 3PM 4, 5pm, 5pm, 6pm, 7pm, 9pm, 10] = > [1Med 3, 5pm, 7pm, 9...] "lst = [1pm, 2pm, 3pm, 4pm, 5pm, 6pm, 7pm, 8" 9] lst_new = [] for i in lst: if I% 2 = = 1: lst_new.append (I) print (lst_new) # rewrite push lst = [i for i in lst if i% 2 = = 1] print (lst) # 3. Multi-loop push # who ♡♢♤♠ who lst1 = [Sun Jielong "," Chen Lu "," Cao Jingyi "] lst2 = [" Wang Zhiguo "," Deng Peng "," reasonable "] lst_new = [] for i in lst1: for j in lst2: lst_new.append (I +" ♡♢♤♠ "+ j) print (lst_new) # rewrite push lst = [I +" ♡♢♤♠ "+ j for i in lst1 for j in lst2] print (lst) # 4. Multi-loop deduction lst_new with judgment condition lst_new = [] for i in lst1: for j in lst2: if lst1.index (I) = lst2.index (j): lst_new.append (I + "♡♢♤♠" + j) print (lst_new) # rewrite deductive lst = [I + "♡♢♤♠" + j for i in lst1 for j in lst2 if lst1.index (I) = = lst2.index (j)] print (lst) 2. Introduction exercise # lead exercise # (1). {'x':'A','y':'B','z':'C'} write the dictionary as dic = {'x','A','y','B','C': for k lst = [] V in dic.items (): res = k + "= + v lst.append (res) print (lst) # deductive lst = [k +" = "+ v for k in dic.items ()] print (lst) # (2). Lowercase ["ADDD", "dddDD", "DDaa", "sss"] lst = ["ADDD", "dddDD", "DDaa", "sss"] lst_new = [] for i in lst: lst_new.append (i.lower ()) print (lst_new) # deductive lst = [i.lower () for i in lst] print (lst) # (3) .x is an even number between 0 and 5. Y is an odd number between 0 and 5, putting XMagol y together into tuples in the list # method-method-lst = [] for x in range (6): if x% 2 = = 0 and y% 2 = = 1: lst.append ((XMague y)) print (lst) # deductive lst = [(x) Y) for x in range (6) for y in range (6) if x% 2 = = 0 and y% 2 = = 1] print (lst) # Methodology II lst = [] for x in range (6): if x% 2 = = 0: for y in range (6): if y% 2 = = 1: lst.append ((xMagy)) print (lst) # derived lst = [(x) Y) for x in range (6) if x% 2 = = 0 for y in range (6) if y% 2 = = 1] print (lst) # (4) Use list deduction to make the operations for i in range (1) in all 99 multiplication tables: for j in range (1): print ("{: d} * {: d} = {: 2d}" .format) print () lst = ["{: d} * {: d} = {: 2d}" .format (1) for j in range (1) 1)] print (lst) # (5) find the product of matrices and elements in Mrector N # M = [1pje 2pas 3], # [4je 5pas 6], # [7pr 8pr 9]] # N = [2pr 2pr 2], # [3pr 3pr 3], # [4pr 4pr 4] M = [1pr 2p3], [4m 5m 6], [7m 8m m 9] N = [2m 2m 2] 2], [3,3,3], [4,4 4]] "" M [0] [0] * N [0] [0] = 2m [0] [1] * N [0] [1] = 4m [0] [2] * N [0] [2] = 6m [1] [0] * N [1] [0] = 12m [1] [1] * N [1] [1] = 15m [1] [2] * N [1] [2] = 18m [2] [0] * N [1] 2] [0] = 12m [2] [1] * N [2] [1] = 15m [2] [2] * N [2] [2] = 18 "" # = > achieve effect 1 [2 4, 6, 12, 15, 18, 28, 32, 36] "" double-layer cycle, the outer cycle moves slowly and the inner cycle moves fast, exactly symbolizing the subscript of M-N matrix "lst = [] for i in range (3): for j in range (3): lst.append (M [I] [j] * N [I] [j]) print (lst) # = > realization effect 2 [2,4,6], [12,15] 18], [28, 32, 36] # iterate through three empty lists lst = [[] for i in range (3)] print (lst) lst = [[M [I] [j] * N [I] [j] for j in range (3)] for i in range (3)] print (lst) "" [M [I] [j] * N [I] [j] for j in range (3)] [2,4] 6] [12, 15, 18] [28, 32, 36] Collection _ dictionary deductive type # case: for people aged 18 to 21 with deposits greater than or equal to 5000 and less than 5500, the card opening format is: honorable VIP card old x (surname), otherwise, the card opening format is as follows: foot-stingy big Han card old x (surname) statistics the types of card opening "" lst = [{"name": "Zhao Shenyang" "age": 18, "money": 3000}, {"name": "Zhao Wanli", "age": 19, "money": 5200}, {"name": "Zhao Fengzong", "age": 20, "money": 100000}, {"name": "Zhao Shichao", "age": 21, "money": 1000}, {"name": "Wang Zhiguo", "age": 18, "money": 5500}, {"name": "Wang Yongfei" "age": 99, "money": 5500}] setvar = set () for i in lst: print (I) # {'name':' Zhao Shenyang', 'age': 18,' money': 3000} if 18 generator gen = mygen () # when the generator is called for the first time, the data cannot be sent because the code location saved by yield is not encountered By default, you can only send None "" res = gen.send (None) print (res, ") # second call to generator res = gen.send (" 100th ") print (res,") # third call to generator res = gen.send ("200") print (res,") # fourth call to generator "errorres = gen.send (" 300 ") print (res,")" call generator using send The first time you send it, it must be None, because you haven't encountered the yield saved code location res = gen.send (None) go to the mygen generator function print ("start") res = yield "Internal 1" execute line 80, save exit, record the current code location, return "Internal 1" to 98 lines to accept data res = "Internal 1" print (Internal 1) The second call to the generator res = gen.send sends the data to line 80 where the last code was saved for acceptance. Cause 80 lines res = 100print 81 lines print (100,") execute 83 lines res = yield "internal 2" save exit, record the current code location, return "internal 2" to execute 102lines res = gen.send ("100th") = > "internal 2" print ("internal 2", ""). And so on and so on. By the fourth call, gen.send (300) could not accept the return value because there was no more yield return data, so an error was reported to stop iterating StopIteration, and the program was terminated "use of" # (4) yield from "" turn an iterable object into an iterator return "def mygen (): lst = [" Zhang Lei "," Li Yafeng "," Liu Yifeng " "Wang Tongpei"] yield from lst# initialization generator function gen = mygen () print (next (gen)) # print (next (gen)) # StopIteration# (5) Fibonacci sequence "use the generator to obtain all the content in segments, instead of printing all the data" 1 1 2 3 5 8 13 21 34. "" def mygen (maxval): a while b = 0pm 1 I = 0 while I
< maxval: # print(b) yield b a,b = b,a+b i += 1# mygen(10)gen = mygen(10)# 第一次获取for i in range(3): print(next(gen))# 第二次获取for i in range(5): print(next(gen))5. Small exercise # 1. Write the following program # (1) in deductive style to build the following list: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] lst = [I * 2 for i in range (10)] lst = [i for i in range (0je 19jin2)] print (lst) # (2) lst = ['alex',' WuSir', 'Old Boy', 'Mystery Boy'] build lst as follows: ['alex0',' WuSir1' Method 1 # lst = [I + str (lst.index (I)) for i in lst] # method 2 lst = [LST [I] + str (I) for i in range (len (lst))] print (lst) # (3) build the following list: [(0,1), (1,2) (2, 3), (3, 4), (4, 5), (5, 6)] lst = [(JLINI) for j in range (0Pie6) for i in range (1PM7) if iRich j = = 1] print (lst) lst = [(iQuin1) for i in range (6)] print (lst) # (4) find the square of the number that is divisible by 3 within 50 And put it in a list. Lst = [I * * 2 for i in range (51) if I% 3 = = 0] print (lst) # (5) M = [[1pje 2 for i in M 3], [4pje 5 for i in M 6], [7 pyrant 8] 9], form a new list M = [[1m 2e 3], [4e 5e 6], [7p 8pr 9] lst = [I [- 1] for i in M] print (lst) # (6) construct the following list: [python 1', 'python 2' 'python 3', 'python 4', 'python 6', 'python 7', 'python 8', 'python 9', 'python 10'] lst = ["python {}" .format (I) for i in range (1m 11) if I! = 5] print (lst) # (7) filter out the list of strings of length less than 3 and convert them to uppercase letters lst = ["sdfsdfsdfsdf", "234", "the symbol you say is", "a" "ab"] lst = [i.upper () for i in lst if len (I) > = 3] print (lst) # (8) except Big and small Wang, there are 52 items, each of which is a tuple. Please return to the following playing card list [('Hearts') '2'), ('grass flowers','J'), … ('spades','A')] lst1 = ["hearts", "grass flowers", "spades", "square pieces"] lst2 = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"] lst = [(iMagazine j) for i in lst1 for j in lst2] print (lst) # 2. Write the following program lst1 = {'name':'alex',' Values': [{'timestamp': 1517991992.94 timestamp': 1517992000.94 timestamp': 1517992000.94 {'timestamp': 1517992014.94 timestamp': valuations: 300,}, {' timestamp': 1517992744.94 Convert lst1 into the following lst2:lst2 = [[1517991992.94], [1517992000.94], [1517992014.94], [1517992744.94,350], [1517992800.94,280] # method-lst2 = [I ["timestamp"] I ["values"]] for i in lst1 ["Values"]] print (lst2) # method II lst2 = [list (i.values ()) for i in lst1 ["Values"]] print (lst2) # 3. Read all the contents of a file and get one row of data at a time by calling the generator. Def mygen (filename): with open (filename,mode= "r +") Encoding= "utf-8") as fp: for i in fp: yield i gen = mygen ("ceshi111.txt") for i in range (3): print (next (gen)) for i in range (6): print (next (gen)) # 4. Rewrite the common summation function into yield print (") def add (AMagneb): yield a + bmygen = add (34Power5) for i in mygen: print (I) so far, I believe you have a deeper understanding of" Python full stack derivation and generator implementation ", you might as well come to the actual operation! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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.