In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the python itertools package how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this python itertools package how to use the article will have a harvest, let's take a look.
Foreword:
Python's built-in module itertools provides very useful functions for manipulating iterative objects, and several "infinite" iterators provided by itertools
1. Count () creates a sequence of natural numbers import itertools n = 0natuals = itertools.count (1) for m in natuals: print (m)
Because count (n) creates an infinite iterator, starting with n, iterating to infinity. So the above code will print out a sequence of natural numbers, can not stop at all, can only press Ctrl+C to exit.
Running result:
0 1 2 3 4 5 6 7...
2. Cycle () creates a sequence loop
Cycle () repeats an incoming sequence indefinitely.
Example 1. The contents of the circular list:
Natuals = itertools.cycle ([1,2,3]) for m in natuals: print (m)
Running result:
1 2 3 1 2 3 1 2 3...
Example 2. Character loop in a string:
Import itertoolscs = itertools.cycle ('ABC') # Note that a string is also a for c in cs of a sequence: print (c)
Running result:
A B C A B C A B C...
3. Repeat () repeats an element a finite number of times
Repeat () is responsible for repeating an element indefinitely (the default behavior), but you can limit the number of repeats if you provide a second parameter.
Ns = itertools.repeat ('Aids, 3) for n in ns: print (n)
Running result:
An An A...
4. Takewhile () intercepts a finite sequence according to conditional judgment.
An infinite sequence iterates indefinitely only when for iterates. If you just create an iterative object, it does not generate infinite elements in advance, and in fact it is impossible to create unlimited elements in memory. Although the infinite sequence can be iterated indefinitely, we usually intercept a finite sequence according to the conditional judgment through functions such as takewhile ().
Import itertools natuals = itertools.count (1) ns = itertools.takewhile (lambda x: X
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.