In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to achieve loop traversal in python, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Cycle traversal of python
Let's just go to the code and look at the case:
Import time# deletes all elements surnamed Zhang in the following list, and the output result should be ['Boss Li', 'Laoer Li'] lst = ['Boss Zhang', 'Boss Zhang', 'Boss Li', 'Boss Zhang', 'Laoer Li'] * 1000 direct for loop traversal list Remove needs to delete the element def del1 (lst): for i in lst: if I [0] = = 'Zhang': lst.remove (I) # when deleting lst [0] 'Boss Zhang', the length of the list becomes 4, which causes the value of lst [1] to be 'Boss Li', and the result returned by skipping 'Zhang Laoer' return lst # does not meet expectations # forward traversal By making a copy of the original list, and then traversing the copy, delete the element def del2 (lst): lst2 = lst.copy () # create the copy with high memory and time overhead for i in lst2: if I [0] = = 'Zhang': lst.remove (I) # delete the first matching element, retrieve the matching time is expensive return lst # although the result is correct But the efficiency is very low, do not use this method # use the higher-order function filter method def del3 (lst): def comp (n): # to create a filter function return n [0]! = 'Zhang' # return True for elements whose first character is not 'Zhang' and keep it. Delete if it is returned to False. Return list (filter (comp, lst)) # filter higher-order function deletes elements from the list. # Delete condition is comp method, return iterator, need list method to convert to list # reverse order deletion def del4 (lst): for i in range (len (lst)-1,-1,-1): # Note len (lst) must be-1, because the subscript of list element is 0 to len (lst)-1 Note that the for loop opens left and closes right. # must be-1 from the end of the lst loop to the beginning. Writing 0 will miss lst [0];-1 indicates reverse order. Range is actually an int numeric list generator, and what is actually generated here is # [49999Magne49998. 1P0], which accesses the specified elements of the list through the subscript. If lst [I] [0] = = 'Zhang': del lst [I] while loop deleted in reverse order by return lst#, the effect is the same as that of for sequence number, and the difference in running efficiency is extremely small (the memory cost of for sequence number method is a little higher). The while loop needs to write seven lines, and the # for loop only needs five lines. It is more recommended to use the for loop. But the while loop code is easier to read. Def del5 (lst): length = len (lst)-1 while length > = 0: if lst [length] [0] = = 'Zhang': del lst [length] length-= 1 return lst# lst = del1 (lst) # del1 method directly traverses the list to delete the specified element, and returns the result error # print (lst) # T1 = time.time () # lst = del2 (lst) # del2 by creating a copy of the original list The traversal copy deletes the specified element in the original, and the result is correct, but it is very inefficient # T2 = time.time () # print (f "it takes time for the traversal method to delete the element: {T2-t1pur.5f}") # 4.51529, you can see the need for code optimization, although the results are consistent, but the performance varies greatly. # performance evaluation is generally based on two indicators, 1 is time consumption, 2 is resource consumption (usually refers to memory consumption, there are other resource consumption on special occasions). T1 = time.time () lst = del3 (lst) T2 = time.time () print (f "when the filter method deletes elements: {T2-T1 del4 .5f}") # 0.0059Secrett1 = time.time () # lst = del4 (lst) # T2 = time.time () # print (f "ergodic method deletes elements: {T2-t1rig .5f}") # 0.0799Secrett1 = time.time () # lst = del5 (lst) # T2 = time.time () # print (f "time it takes to delete an element by traversal method: {T2-t1pur.5f}") # 0.08516 finish reading the above Do you have any further understanding of how to implement loop traversal in python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.