python personal notes

Introduction

I hardly remember Python! However, I need to use it often, and I feel that I am investigating the same thing many times, so I will leave a memorandum in the future. I will add it every time I check it.

It may not be useful because it is a personal memo, but please use it if you like.

Type conversion

test_str = '100'
int(test_str)

No line breaks with print ()

print('text', end='')

PATH generation

# add path
path = './'
path = os.path.join(path, 'dir')
# generate path
path = ['./', 'dir']
path = os.path.join(*path_list)

Loop with index number

If you use ʻenumerate ()`, you can get it at the time of loop without writing the addition of the counter.

python


test_list = ['test1', 'test2', 'test3']
for idx, test in enumerate(test_list):
  print('{} : {}'.format(idx, test))

String manipulation

Splitting strings

python


test_str = 'Hello World'
test_str.split() #Separate by space →['Hello', 'World']
test_str.split('o') #'o'Separated by →['Hell', ' W', 'rld']

String concatenation

python


test_str = 'Hello World'
test_list = test_str.split()
test_join = ''
test_join = test_join.join(test_list) #Between`test`Put and combine →'HelloWorld'
test_join = ' '
test_join = test_join.join(test_list) #Between`test`Put and combine →'Hello World'

String replacement

python


test_str = 'Hello World'
test_str.replace('o', 'p') #'o'To'p'Convert to →'Hellp Wprld'
test_str.replace('o', 'p', 1) #only 1 time'o'To'p'Convert to →'Hellp World'

dictionary

Delete key

If you just want to delete it, you only need the second line, but considering that the key does not exist, it seems safer to keep it in the second line.

if 'key' in dic:
  del dic['key']

If you try to delete the key inside the loop, you will get an error, so do the following:

for key in list(dic):
  if ...: #Favorite conditions
    del dic[key]

math

import

import math
from math import * ## math.No need to put on

Formula

math.sin(radian)
math.cos(radian)
math.tan(radian)
math.exp(x)
math.log(x)
math.pow(x, y)
math.sqrt(x)

constant

math.pi
math.e

numpy

import

import numpy as np

Matrix generation

#Zero matrix generation
zeros_array = np.zeros((row, column))
#Ichi matrix generation
ones_array = np.ones((row, column))
#Numeric matrix generation
value_array = np.full((row, column), value)

inner product

np.dot(A, B)

OpenCV

import

import cv2

Load image

img = cv2.imread(path)

Draw image

cv2.imshow('img', img)
cv2.waitKey(0) #The argument is the drawing time
cv2.destroyAllWindows()

Write image

cv2.imread(path, img)

reference

Recommended Posts

python personal notes
missingintegers python personal notes
[Personal notes] Python, Django
Python scraping notes
Python study notes _000
Python beginner notes
Python study notes_006
Personal notes for python image processing
Python Pandas Data Preprocessing Personal Notes
Python study notes _005
Python grammar notes
Python Library notes
(Personal notes) Python metaclasses and metaprogramming
python pandas notes
Python study notes_001
python learning notes
Python3.4 installation notes
Pandas Personal Notes Summary
Python package development notes
Python memorandum (personal bookmark)
python decorator usage notes
Python ipaddress package notes
Python Pickle format notes
[Python] pytest-mock Usage notes
First Python miscellaneous notes
Matlab => Python migration notes
Notes around Python3 assignments
Notes using Python subprocesses
Python try / except notes
Python framework bottle notes
Personal notes to doc Python code in Sphinx
O'Reilly python3 Primer Learning Notes
Web scraping notes in python3
Python
Python standard unittest usage notes
Python notes to forget soon
python * args, ** kwargs Usage notes
Manipulate excel files from python with xlrd (personal notes)
Python 處 處 regular expression Notes
[Python] Notes on data analysis
Python data analysis learning notes
Notes on installing Python on Mac
[AtCoder] ABC165C Personal Note [Python]
Get Evernote notes in Python
Notes on installing Python on CentOS
Notes on Python and dictionary types
[Personal memo] Python virtual environment command memo
Notes on using MeCab from Python
Laplacian eigenmaps with Scikit-learn (personal notes)
Python package management tool personal summary
Git & Github & python & VScode Personal memorandum
Typing automation notes by Python beginners
Notes on installing Python using PyEnv
[Personal memo] Python sequence type / mapping type
Notes for Python file input / output
Notes on using rstrip with python.
Notes on accessing dashDB from python
Minecraft boot options [Linux] Personal notes
kafka python
Notes for using OpenCV on Windows10 Python 3.8.3.
Notes using cChardet and python3-chardet in Python 3.3.1.