3/19/2019

Python hstack, vstack example code

import numpy as np

#hstack #1
a = np.array((1,2,3))
b = np.array((2,3,4))
print(np.hstack((a,b)))
>
 [1 2 3 2 3 4]

#hstack #2
a = np.array([[1],[2],[3]])
b = np.array([[2],[3],[4]])
print(np.hstack((a,b)))
>
 [[1 2]
 [2 3]
 [3 4]]

#vstack #1
a = np.array([1, 2, 3])
b = np.array([2, 3, 4])
print(np.vstack((a,b)))
>
 [[1 2 3]
 [2 3 4]]


#vstack #2
a = np.array([[1], [2], [3]])
b = np.array([[2], [3], [4]])
print(np.vstack((a,b)))
>
[[1]
 [2]
 [3]
 [2]
 [3]
 [4]]

3/07/2019

python number list, remove duplication and sort

coordiX = [0, 5016, 40, 5012, 40, 5012, 40, 5012, 40, 5012, 3169, 4970, 3169, 4970, 3169, 4970, 3169, 3537, 3586, 4355, 4395, 4970, 2632, 4616, 2632, 4616, 2632, 4616, 2632, 4616, 3651, 3659, 3651, 3659, 3651, 3659, 3651, 3659, 2630, 4614, 2630, 4614, 2630, 4614, 2630, 4614, 2632, 4616, 2632, 4616, 2632, 4616, 2632, 4616, 2630, 4614, 2630, 4614, 2630, 4614, 2630, 4614, 2632, 4614, 2632, 4614, 2632, 4614, 2632, 4614, 2630, 4614, 2630, 4614, 2630, 4614, 2630, 4614, 2630, 2640, 2630, 2640, 2630, 2640, 2630, 2640, 3652, 3660, 3652, 3660, 3652, 3660, 3652, 3660, 328, 4670, 328, 4670, 328, 4670, 328, 4670, 328, 4668, 328, 4668, 328, 4668, 328, 4668, 330, 4614, 330, 4614, 330, 2962, 330, 784, 808, 1206, 2694, 2962, 2692, 3868, 2692, 3404, 3428, 3868, 332, 866]

print("origin")
print(coordiX)
print("length:", len(coordiX))

print("remove duplication")
coordiX = list(set(coordiX))
print(coordiX)
print("length:", len(coordiX))

print("sort")
coordiX.sort()
print(coordiX)

output
origin
[0, 5016, 40, 5012, 40, 5012, 40, 5012, 40, 5012, 3169, 4970, 3169, 4970, 3169, 4970, 3169, 3537, 3586, 4355, 4395, 4970, 2632, 4616, 2632, 4616, 2632, 4616, 2632, 4616, 3651, 3659, 3651, 3659, 3651, 3659, 3651, 3659, 2630, 4614, 2630, 4614, 2630, 4614, 2630, 4614, 2632, 4616, 2632, 4616, 2632, 4616, 2632, 4616, 2630, 4614, 2630, 4614, 2630, 4614, 2630, 4614, 2632, 4614, 2632, 4614, 2632, 4614, 2632, 4614, 2630, 4614, 2630, 4614, 2630, 4614, 2630, 4614, 2630, 2640, 2630, 2640, 2630, 2640, 2630, 2640, 3652, 3660, 3652, 3660, 3652, 3660, 3652, 3660, 328, 4670, 328, 4670, 328, 4670, 328, 4670, 328, 4668, 328, 4668, 328, 4668, 328, 4668, 330, 4614, 330, 4614, 330, 2962, 330, 784, 808, 1206, 2694, 2962, 2692, 3868, 2692, 3404, 3428, 3868, 332, 866]
length: 130

remove duplication
[0, 3586, 4355, 2692, 4614, 2694, 4616, 784, 2962, 5012, 5016, 3868, 40, 808, 4395, 1206, 4668, 4670, 3651, 3652, 2630, 2632, 328, 330, 3659, 3660, 3404, 332, 2640, 3537, 3169, 866, 3428, 4970]
length: 34

sort
[0, 40, 328, 330, 332, 784, 808, 866, 1206, 2630, 2632, 2640, 2692, 2694, 2962, 3169, 3404, 3428, 3537, 3586, 3651, 3652, 3659, 3660, 3868, 4355, 4395, 4614, 4616, 4668, 4670, 4970, 5012, 5016]

python 2d array and rows and cols

#make array
W = 2
H = 3


list2d = []
for i in range(0,H):
w_list =[]
for j in range (0,W):
w_list.append((i,j))
list2d.append(w_list)


#print 2d array
print(list2d)


#get row and col
Height = Rows = len(list2d)
Width = Cols = len(list2d[0])


#check values
print( Rows, Cols)
print( Height, Width)

#print all elements
for i in range(0,Rows):
for j in range (0,Cols):
print(list2d[i][j])


output
[[(0, 0), (0, 1)], [(1, 0), (1, 1)], [(2, 0), (2, 1)]]
3 2
3 2
(0, 0)
(0, 1)
(1, 0)
(1, 1)
(2, 0)
(2, 1)

3/06/2019

python dic to json, json to txt file (example source code)

This article is about how to convert dic type to json.

source code is like that:
Dic -> Json -> txt file 1
txt file 1 -> Json -> Dic -> Json -> txt file 2

So consequentially, txt file1 and txt file 2 would be same.

Then check source code.

#dictionary type
dic_type = {'dic_type': 'yes', 'json': 10}

#dic to json
import json
str_type = json.dumps(dic_type) #dic to json

#write json to file
f= open("./json.txt","w+")
f.write(str_type)
f.close

#dic from json
dic_type_from_json = json.loads(str_type)

#dic from file
f = open("./json.txt","r")
str_type_from_file = f.read()
f.close
dic_type_from_file = json.loads(str_type_from_file)

#check data type
print( type(str_type) )#Output str
print( type(dic_type_from_json) )#Output dict
print( type(dic_type_from_file) )#Output dict


#write json to file
f= open("./json2.txt","w+")
r = json.dumps(dic_type_from_file)
f.write(r)
f.close

#json.txt and json2.txt is same