Original link :Hzy Blog
count() Take two parameters
from itertools import count
"""
Infinite iterators count()
"""
c = count(0, 2)
v = next(c)
while v < 10:
v = next(c)
print(v, end=',')
cycle It's one while True, The numbers in the infinite loop .
"""
Infinite iterators cycle()
"""
from itertools import cycle
c = cycle('ABCD')
for i in range(10):
print(next(c), end=',')
Repeat iteration elem,n Time
"""
Infinite iterators repeat()
"""
from itertools import repeat
r = repeat(1, 3)
for i in range(3):
print(next(r), end=',')
Use func The function of iterates over the object p Accumulate .
"""
iterator accumulate()
"""
from itertools import accumulate
test_list = [i for i in range(1, 11)]
for i in accumulate(test_list): # The default is operator.add
print(i, end=',')
print()
for i in accumulate(test_list, lambda x, y: x * y): # operator.mul
print(i, end=',')
chain() Multiple iteration objects can be placed in the , Then iterate one by one .
"""
iterator chain()
"""
from itertools import chain
ch = chain([1, 2, 3], {4: 4, 5: 5}, {6, 7, 8}, (9,), [10, [11, 12]])
for i in ch:
print(i)
Follow chain The difference is this :
"""
iterator chain.from_iterable()
"""
def gen_iterables():
for i in range(10):
yield range(i)
for i in chain.from_iterable(gen_iterables()):
print(i)
That's it. Just look at this s yes selectors The elements in .
(d[0] if s[0]), (d[1] if s[1]), ...
"""
iterator compress
"""
from itertools import compress
print(list(compress(['A', 'B', 'C', 'D'], [0, 1, 1, 1])))
The condition for the beginning of the cycle is , Until I met
for the first time
dissatisfaction pred Conditions , Just started traversing .
"""
iterator dropwhile()
"""
from itertools import dropwhile
l = [1, 7, 6, 3, 8, 2, 10]
print(list(dropwhile(lambda x: x < 3, l)))
This feeling is very interesting , It's kind of like sql Medium group_by. It can be used on strings , List, etc .
from itertools import groupby
# Group strings
for k, g in groupby('11111234567'):
print(k, list(g))
d = {1: 1, 2: 2, 3: 2}
# According to the dictionary value To group
for k, g in groupby(d, lambda x: d.get(x)):
print(k, list(g))
This is to cut the iterative object , Negative numbers are not supported , It's kind of like range(1,10,2) such
from itertools import islice
print(list(islice('ABCDEFG', 2,3, None)))
This and zip It's like , The difference is :
from itertools import zip_longest
for x,y in zip_longest([1,2,3],[1,2]):
print(x,y)
for x,y in zip([1,2,3],[1,2]):
print(x,y)
amount to Nested for
“”"
Permutation and combination iterators product Nested for
“”"
from itertools import product
for i,j in product([1,2,3],[4,5]):
print(i,j
Full Permutation , Like output 123 All about .(1,2,3),(1,3,2)…
from itertools import permutations
print(list(permutations('123')))
from p Find all the length in the r The arrangement of … In order
from itertools import combinations
print(list(combinations([1,2,3],2)))
from p Find all the length in the r The arrangement of , In order , But including itself means repetition .
Hello, everyone , Im a panda
Take the absolute value a = ab