I will leave it as a memorandum.
Get ASCII code with ʻord`
>>> ord('a')
97
Get characters with chr
>>> chr(97)
a
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))
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
str1 = "a,b,c"
sep = str1.split(",")
print(sep)
## ['a', 'b', 'c’]
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]
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)
I would like to add more from time to time. If you have any suggestions, please do not hesitate to contact us.
Recommended Posts