The power of python's built-in features: Itertools


When I first learned python, I felt that the syntax [for i in range(10)] was so powerful that I no longer had to write for loops like C. Iterators are very versatile in python, except for iterators like range, which have the huge advantage of being fast and having a very low memory footprint. More iterators are provided in python's built-in Itertools, which are efficient to use and have more elegant code.

There are 3 types of iterators available in Itertools.

I. Infinite types

Infinite types are, as the name implies, iterable all the time

count()

Counter, two parameters to this method, iteration start value start, step value step.

cycle()

Looper, a method with one argument, provides an iterable object, and then provides a loop iterator.

repeat()

Repeat, this method two parameters, the first is to pass in an object, and then always repeat to return this object, followed by times is an optional parameter that indicates the number of times to repeat, the default is infinite.

II. Generation by sequence

This type is passed in a finite sequence that is transformed to return an iterator

accumulate()

accumulate means accumulate and accepts two arguments, the first being an iterable object and the second being optional, a method to operate on the previous iterator, which defaults to additive.

chain()

The chain accepts parameters that are variable and then strings the incoming parameters together and returns them one by one.

chain.from_iterable()

It functions the same as chains, the difference being that the parameters received are iterable objects.

compress()

compress means compress, it receives two parameters, the first data is the data being manipulated, the second parameter is the selector, data and selectors position one by one, when the selectors in True, the corresponding element in data will return, if False then skip.

dropwhile()

dropwhile takes two arguments, the first is a function and the second is an iterable object that starts returning content when the content in the iterable object is passed into the first function that returns false, otherwise it is discarded.

filterfalse()

filterfalse takes two arguments, the first is a function, the second is an iterable object, when the content of the iterable object is passed into the first function returns false then the content passed in is returned, otherwise it is discarded.

groupby()

groupby receives two parameters, the first parameter is an iterable object, the second is the grouping method, it is an optional parameter, the default is to return to itself, groupby and SQL groupby is not the same, it is based on the order, will not look at the whole sequence after the grouping, as long as the front and back of the two elements are not the same grouping, and the grouping generated by the previous iteration will disappear, need to store themselves.

islice()

islice is an iterator that generates slices, and the usage is very simple to understand by looking at the example above.

starmap()

starmap takes two arguments, the first is a function, the second is an iterable object, in turn the elements of the iterable object are passed into the function, and then the result of the function's computation is returned, the difference between it and map is that it can take variable arguments.

takewhile()

takewhile the first argument is a conditional judgment, the second is an iterable object, and in turn the elements of the iterable object are passed into the Predicate, which returns if true and aborts the iteration once it is false.

tee()

The role of tee is to generate multiple iterators with an iterable object. When using tee later, the original iterable object should be avoided to be used again, and if the amount of data stored temporarily is too large then list should be used.

zip_longest()

Same functionality as zip, but it can combine multiple iterable objects

iii. permutation iterators

This series of iterators is very useful because there are many scenarios where we need to permute data.

product()

product can combine multiple input sequences in a permutation, with the combination being a Cartesian product.

permutations()

permutations will consider the order of combinations

combinations()

combinations do not consider the order and do not put back data.

combinations_with_replacement()

combinations_with_replacement does not consider order and has putback data.


Recommended>>
1、Which direction is the future of Chinas chicken industry really heading
2、The friend circle bull cheat friend circle bull seethrough software friend circle bull hang aids
3、What about humans getting their jobs taken by AI Nobel laureate in economics livelihood insurance may cut losses
4、Python Language Programming Series 007 PyQt Custom Controls for Color Boxes
5、Data Analytics and Artificial Intelligence the new golden key to studyjob search

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号