It's too detailed to write Python-related tips one by one, so I'll summarize them here.
Algorithm
Unique
For a sequence, create a sequence with only unique elements. The code below is from Stack OverFlow:
def uniquify(seq):
seen = set()
seen_add = seen.add
return [x for x in seq if x not in seen and not seen_add(x)]
[Wandbox] Go to Sanhe (he՞ ਊ՞) ha ha
This is interesting. You can only touch the bound methods.
-Python Tips: I want to remove duplicate elements from the list --Life with Python
Split
From a certain sequence, create a sequence divided by length n. The code below is based on here:
l = 10
n = 4
zip(*[iter(range(l))]*n)
import itertools
itertools.izip_longest(*[iter(range(l))]*n)
import more_itertools
more_itertools.chunked(range(l), n)
[Wandbox] Go to Sanhe (he՞ ਊ՞) ha ha
-Split the list into n sublists (Python) --Ogirogu Hatebro
Once
Even if a function is called many times, it is processed only the first time.
def run_once(f):
def wrapper(*args, **kwargs):
if not wrapper.has_run:
wrapper.has_run = True
return f(*args, **kwargs)
wrapper.has_run = False
return wrapper
@run_once
def greeting():
print('Hello.')
greeting()
greeting()
greeting()
[Wandbox] Go to Sanhe (he՞ ਊ՞) ha ha
Exclusive processing will be required for parallel calls. The code above does not take this into account. Therefore, I feel that this code is bad know-how.
TODO: Need improvement
Reversed numpy.cumsum
Recommended Posts