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 > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "what are the methods for creating a python generator". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the methods of creating a python generator.
1. Deductive method, just change the [] generated by the list to ()
There are many ways to create a generator.
In [26]: l = [num * 2 for num in range (5)] In [27]: LOut [27]: [0,2,4,6,8] In [28]: G = (num * 2 for num in range (5)) In [29]: GOut [29]:
2. Next () function
In [30]: next (G) Out [30]: 0 In [31]: next (G) Out [31]: 2 In [32]: next (G) Out [32]: 4 In [33]: next (G) Out [33]: 6 In [34]: next (G) Out [34]: 8 In [35]: next (G)-- -- StopIteration Traceback (most recent call last) in-> 1 next (G) StopIteration:
3. For loop and list, because G has iterated to the end of the ipython test, so you need to rebuild G, otherwise there will be no data.
In [38]: G = (num * 2 for num in range (5)) In [39]: for i in G:...: print (I)...: 02468 In [40]: list (G) Out [40]: [] In [41]: G = (num * 2 for num in range (5) In [42]: list (G) Out [42]: [0,2,4,6,8] so far I believe that you have a deeper understanding of "what are the methods of creating python generators?" you might as well do it in practice. 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.