Python

package-SpeciFIC: JUPYTER/IPYTHON, PANDAS, MATPLOTLIB, H5PY

List Splicing

combine two lists with alternating order


# combination does not work if two lists are of different sizes
list1=[1,2,3]
list2=[4,5,6]
combined_list=[0]*len(list1)*2
combined_list[::2]=list1
combined_list[1::2]=list2

List Generation

generate parameter search list


# generate n points around xo with a step of dx
import numpy as np
x=np.array([i*dx for i in range(n)]) # generate points with proper separation
x=x+xo-x.mean() # shift mean to desired value

generate uniform or Chebyshev grid for fitting


# generate an n-point uniform grid from 2 to 4
x = np.linspace(2,4,n)
# generate an n-point Chebyshev grid from 2 to 4
x = [np.cos( (2*k+1)*np.pi/(2*n) ) for k in range(n)] # 0 to 1
x = (x-0.5)*2+3 # shift origin to 0, stretch, shift origin to 3

String Manipulation

format complex numbers

"complex number z={:2.2f}".format(z)

Matrix Manipulation

reshape and transpose (permute indeices)

test = np.zeros(1,2,3)
print test.shape
test = test.transpose((1,0,2))
print test.shape
test.reshape(2,3)