1. Introduction
In Python development, theitertools
The library is often overlooked, but in fact it contains some great functions, especially for streaming data. In this article, we'll discuss a few of the library's most used functions and highlight when we should consider using them.
Without further ado, let's get right to it!
2. accumulate() function
third-party repositoryitertools
supplied functionaccumulate()
that can help us perform cumulative operations on a stream of data. In other words, suppose we have a list of data [a, b, c, d, e] and an operation f, then the functionaccumulate()
can help us calculatef(a,b)
,f(f(a,b),c)
,f(f(f(a,b),c),d)
And so on.
Text is a bit unintuitive, so let's take a cumulative chestnut!
The sample code is as follows:
import itertools data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8] res = (data, lambda x,y:x+y) print(list(res)) # [3, 7, 13, 15, 16, 25, 25, 32, 37, 45]
above functionaccumulate()
The effect of this is that you first add 3 and 4 to get 7, then add 6 to get another value, and so on.
Note that this function is not allowed if you want to perform an accumulate operation given 3 or more values, because accumulate() only accepts iterators, which return up to 1 element per call to next().
3. compress() function
function (math.)compress()
The content can be filtered according to our preferences. With the functionfilter()
Functions differ in that the functioncompress()
The appropriate flag bits need to be passed in to determine if each value should be retained.
An example will be more intuitive, the relevant sample code is as follows:
import itertools data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8] selector = [1, 0, 0, 0, 1, 1, 0, 1, 1, 0] res = (data, selector) print(list(res)) # [3, 1, 9, 7, 5]
In the above example, the selector will be 1 if the data is odd, and 0 otherwise.Therefore, the functioncompress()
The result of the operation will simply be to preserve the odd numbers in the original data.
4. groupby() function
In many cases, we'll get a list of tuples in Python in random order if we want to group them by value. In this case, it's the function groupby()
The perfect scenario to play a role!
function (math.) groupby()
will accept a function with iterable arguments and a return value, and then it will group the list of tuples by the value returned by the function.
For example, we would like to group the following cities by country:
import itertools data = [('New York', 'US'), ("Shanghai", "China"), ("LA", 'US'),("Chongqing", "China")] for city, group in (sorted(data, key=lambda x: x[1]), lambda x: x[1]): for i in group: print("%s is in %s." % (i[0], city)) print("") # Shanghai is in China. # Chongqing is in China. # New York is in US. # LA is in US.
5. Ranking and combining operations
The permutations may beitertools
One of the most amazing functions in the library provides permutation operators!
The only thing we need to do is pass the correct keyword to the appropriate function along with the length of the output tuple.
As shown in the example below:
import itertools data = [3, 4, 6] com_res = (data, 2) print(list(com_res)) # [(3, 4), (3, 6), (4, 6)] com_res = (data, 2) print(list(com_res)) # [(3, 4), (3, 6), (4, 3), (4, 6), (6, 3), (6, 4)] com_without_replacement_res = itertools.combinations_with_replacement(data, 2) print(list(com_without_replacement_res)) # [(3, 3), (3, 4), (3, 6), (4, 4), (4, 6), (6, 6)] product_res = (data, data) print(list(product_res)) # [(3, 3), (3, 4), (3, 6), (4, 3), (4, 4), (4, 6), (6, 3), (6, 4), (6, 6)]
6 Summary
storehouseitertools
An underutilized or even seldom heard of library for most people, but it does contain some very nice functions. These functions can often help us reduce the number of lines of code to just one, making our code look cleaner and more elegant!
to this article on the Python itertools library of the four functions introduced to this article, more related to Python itertools library content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!