itertools: Python3 iteration library (continuously updated ing...)

The gods are silent 2022-09-09 00:22:47 阅读数:397


Gods are silent-personal CSDN blog directory

The itertools library is a Python3 library for generating iterable objects.
Official documentation: itertools — Functions creating iterators for efficient looping — Python 3.10.7 documentationp>

Last update time: 2022.9.7
Earliest update time: 2022.9.7

1. combinations(iterable, r)

Each element in the returned iterable is a combination of iterable elements (generated in the order of iterables), with length r.
There is no combination of the element in the iterable and this element (no self-loop), and does not contain repeated elements in the list.


from itertools import combinationsa = ['h', 'y', 'k', 'q', 's']for i in combinations(a, 2):print(i)


('h', 'y')('h', 'k')('h', 'q')('h', 's')('y', 'k')('y', 'q')('y', 's')('k', 'q')('k', 's')('q', 's')

2. Internet sources used in the writing of this article

  1. The use of itertools.combinations() in Python.combination
版权声明:本文为[The gods are silent]所创,转载请带上原文链接,感谢。