Python and numpy tips

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.

Reference information

-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

Reference information

-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

Reference information

Reversed numpy.cumsum

Recommended Posts

Python and numpy tips
Python / Numpy np.newaxis thinking tips
python tips
numpy tips
python tips
Python Tips
Python tips
[Tips] First-order difference calculation and inverse conversion [python / numpy]
Python Conda Tips
Python debugging tips
Python click tips
Unexpectedly (?) Python tips
list and numpy
#Python basics (#Numpy 1/2)
#Python basics (#Numpy 2/2)
Python #Numpy basics
[Python] Numpy memo
Python and NumPy numeric type inheritance relationship
[python] Compress and decompress
Install and run Python3.5 + NumPy + SciPy on Windows 10
[Python] pip and wheel
Batch design and python
Python iterators and generators
Python numpy ignores very small values ​​and displays
Python basics 8 numpy test
Python packages and modules
Vue-Cli and Python integration
[Python] Swapping rows and columns in Numpy data
Ruby, Python and map
[Python] Search (NumPy) ABC165C
python numpy array calculation
Python Tips (my memo)
python input and output
Python and Ruby split
Python PyTorch install tips
[Python] Sorting Numpy data
[Blender x Python] Blender Python tips (11/100)
Python3, venv and Ansible
Python asyncio and ContextVar
Python Basic --Pandas, Numpy-
Tips and precautions when porting MATLAB programs to Python
It's not easy to write Python, it's easy to write numpy and scipy
Encryption and decryption with Python
Python: Class and instance variables
3-3, Python strings and character codes
Python 2 series and 3 series (Anaconda edition)
Python and hardware-Using RS232C with Python-
Convert numpy int64 to python int
Python on Ruby and angry Ruby on Python
Python indentation and string format
Python real division (/) and integer division (//)
Install Python and Flask (Windows 10)
About python objects and classes
About Python variables and objects
Apache mod_auth_tkt and Python AuthTkt
Å (Ongustromu) and NFC @ Python
[Python] Calculation method with numpy
Understand Python packages and modules
# 2 [python3] Separation and comment out
Python shallow copy and deep copy
Implemented SMO with Python + NumPy