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()
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()
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()
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()
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()
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)]
The Pandas with-out question the problem, we want more and more output, including Numpy.
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