Python Computation Library Cheat Sheet ~ itertools ~

Why?

I couldn't get a clue to solve it if I didn't know it when I did a full search in competitive programming, so I decided to take this opportunity to hold down Pandas and Numpy little by little. For the time being, I had a problem with coordinates this time, so I extracted the following from itertools.

itertools.count()

Code written

import itertools

# count()It is possible to return the number specified in the second argument from the number specified in the first argument in.
list1 = []
for i in itertools.count(0,2):
    list.append(i)
    if i == 20:
        break
print(list1) # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

#You can also set a small number
list2 = []
for i in itertools.count(0,2.5):
    list2.append(i)
    if i == 20:
        break
print(list2) # [0, 2.5, 5.0, 7.5, 10.0, 12.5, 15.0, 17.5, 20.0]



itertools.cycle()

Code written

import itertools

# cycle()Keeps copying the iterable specified by the argument indefinitely.
sample = []
count = 0
text = 'TEST'
for i in itertools.cycle(text):
    sample.append(i)
    count += 1
    #For example count==If it is 3, sample= ['T', 'E', 'S']Becomes
    if count == len(text)*4:
        break
print(sample) # ['T', 'E', 'S', 'T', 'T', 'E', 'S', 'T', 'T', 'E', 'S', 'T', 'T', 'E', 'S', 'T']


itertools.accumulate()

Code written

import itertools

# accumulate()Performs the following processing for the iterable specified by the argument
sample = range(1,6)

result = list(itertools.accumulate(sample))
print(result) # [1, 3, 6, 10, 15]

#In other words[sample[0], sample[0]]+sample[1], sample[0]]+sample[1]+sample[2] .......]That means


itertools.chain()

Code written

import itertools

# chain()Can combine the iterable specified by the argument
sample = 'ABC'
sample2 = 'DEF'

result = list(itertools.chain(sample, sample2))
# join()Unlike, it stores one character at a time
print(result) # ['A', 'B', 'C', 'D', 'E', 'F']

#Click here if you want to use it for the list

sample3 = ['ABC', 'DEF']

result = list(itertools.chain.from_iterable(sample3))
print(result) # ['A', 'B', 'C', 'D', 'E', 'F']


itertools.combinations()

Code written

import itertools

# combinations()Returns the combination of the iterable specified by the argument with the number specified by the second argument.

l = [4,4,9,7,5]
new_list = []

#Combinations for duplicate ants_with_replacement()Write
for i in itertools.combinations(l,3):
    new_list.append(i)
print(new_list) # [(4, 4, 9), (4, 4, 7), (4, 4, 5), (4, 9, 7), (4, 9, 5), (4, 7, 5), (4, 9, 7), (4, 9, 5), (4, 7, 5), (9, 7, 5)]




Finally

The Pandas with-out question the problem, we want more and more output, including Numpy.

reference

I'm glad that the official documentation and the following articles are concisely organized.

itertools --- Iterator generation function for efficient loop execution Introduction of itertools, more-itertools

Recommended Posts

Python Computation Library Cheat Sheet ~ itertools ~
Python3 cheat sheet (basic)
PySpark Cheat Sheet [Python]
Python sort cheat sheet
Python evolutionary computation library Deap
Python Evolutionary Computation Library Deap (3)
Data Science Cheat Sheet (Python)
Python Django Tutorial Cheat Sheet
Python Evolutionary Computation Library Deap (2)
Apache Beam Cheat Sheet [Python]
Python cheat sheet (for C ++ experienced)
AtCoder cheat sheet in python (for myself)
Blender Python Mesh Data Access Cheat Sheet
Mathematical Optimization Modeler (PuLP) Cheat Sheet (Python)
Python ast library
Curry cheat sheet
SQLite3 cheat sheet
pyenv cheat sheet
Computation drill python
[Updating] Python Syntax cheat sheet for Java shop
conda command cheat sheet
PIL / Pillow cheat sheet
Linux command cheat sheet
python algorithmic trading library
[Python] Search (itertools) ABC167C
ps command cheat sheet
Spark API cheat sheet
Install python external library
Python optimization library Pulp
Go language cheat sheet
Computation drill python (extension)
Python pdf cheat sheets
Aim python library master (48) autopep8
tox configuration file cheat sheet
Aim python library master (49) psidialogs
Aim python library master (55) namespaces
Aim python library master (46) browserplus
Aim python library master (30) chronyk
numpy memory reuse cheat sheet
Aim python library master (3) workalendar
Aim python library master (42) speedrecorder
Aim python library master (37) slimurl
Recommendation of binpacking library of python
Aim python library master (44) pynetviz
Aim python library master (8) rolex
Aim python library master (7) numparser
Aim python library master (21) hy
Aim python library master (18) requests
Windows10: Install python dlib library
Aim python library master (13) easydev
Aim python library master (20) pyyaml
Aim python library master (40) wordsegmentation
Aim python library master (43) cpmoptimize
Aim python library master (68) pazudorasolver
Aim python library master (58) faker
Aim python library master (11) nlist
Aim python library master (38) beautiful_print
Aim python library master (65) geopy
Aim python library master (2) vincenty
Aim python library master (59) logbook
Aim python library master (51) pyautogui