python tips

python tips summary

I will leave it as a memorandum.

Character → ascii. conversion from ascii to characters

Get ASCII code with ʻord`

>>> ord('a')
97

Get characters with chr

>>> chr(97)
a

List generation

If you write in a comprehensive list, the execution speed is fast because it is not saved in memory.

num = 1000000000

#Normal writing
list_1 = []
for i in range(num):
   list_1.append(i)

#List comprehension
list_2  = [i for i in range(num)]

In addition, @konandoiruasa advised that the following items are based on the inclusion notation and are slightly faster.

import numpy as np
a = list(np.arange(num, dtype=int))

"Join" to join various things

You can join strings, numbers, and paths with join. Use with " ". join.

list = ['a', 'b', 'c']
result = ''.join(list)
print(result)

## abc

If list has letters and numbers, convert them all to str with map function

list = ['a', 'b', 1 ]
result = map(str, list)
result1 = ''.join(result)
print(result1)

## ab1

Split with split

str1 = "a,b,c"
sep = str1.split(",")
print(sep)
## ['a', 'b', 'c’]

Conversion of np.array and list


import numpy as np
list = [1,2,3]

a = np.array(list)
print(a)
## array([1, 2, 3])

b = a.tolist()
print(b)
##[1, 2, 3]

How to export to csv


import pandas as pd

log = pd.DataFrame(
         columns=['epoch', 'lr', 'train_loss', 'val_loss']
         )

tmp = pd.Series([ epoch, lr, train_loss, val_loss ], index=log.columns) 
  
log = log.append(tmp, ignore_index=True) 
log.to_csv('log.csv'), index=False) 

At the end

I would like to add more from time to time. If you have any suggestions, please do not hesitate to contact us.

Recommended Posts

python tips
python tips
Python Tips
Python tips
Python Conda Tips
Python debugging tips
Python click tips
Unexpectedly (?) Python tips
Python and numpy tips
Python
Python Tips (my memo)
Python PyTorch install tips
[Blender x Python] Blender Python tips (11/100)
Python / Numpy np.newaxis thinking tips
Receiving standard input tips @ python
[Tips] Handle Athena with Python
[Python + Selenium] Tips for scraping
Google Drive Api Tips (Python)
~ Tips for beginners to Python ③ ~
kafka python
Python basics ⑤
python + lottery 6
Built-in python
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
Python service
python function ①
Python basics
Python memo
Tips on Python file input / output
ufo-> python (3)
Python comprehension
Jupyter Tips 4
install python
LaTeX, Python tips in master's thesis
Python Singleton
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Python increment
atCoder 173 Python
[Python] function
Python installation
numpy tips
Installing Python 3.4.3.
Try python
Python memo
Python iterative
Jupyter Tips 5
Python algorithm
Python2 + word2vec
Scapy Tips
[Python] Variables
Python functions
Python sys.intern ()
Python tutorial