1. 程式人生 > 實用技巧 >乾貨丨DolphinDB超程式設計教程

乾貨丨DolphinDB超程式設計教程

技術標籤:Python資料分析

collections namedtuple

Python中儲存系列資料,比較常見的資料型別有list,除此之外,還有tuple資料型別。相比與list,tuple中的元素不可修改,在對映中可以當鍵使用。tuple元組的item只能通過index訪問,collections模組的namedtuple子類不僅可以使用item的index訪問item,還可以通過item的name進行訪問。可以將namedtuple理解為c中的struct結構,其首先將各個item命名,然後對每個item賦予資料。

coordinate = namedtuple('Coordinate'
, ['x', 'y']) co = coordinate(10,20) print co.x,co.y print co[0],co[1] co = coordinate._make([100,200]) print co.x,co.y co = co._replace(x = 30) print co.x,co.y
results:
10 20
10 20
100 200
30 200
from collections import namedtuple
 
websites = [
    ('Sohu', 'http://www.google.com/', u'張朝陽'),
    ('Sina', 'http://www.sina.com.cn/'
, u'王志東'), ('163', 'http://www.163.com/', u'丁磊') ] Website = namedtuple('Website', ['name', 'url', 'founder']) for website in websites: website = Website._make(website) print website
results:

Website(name='Sohu', url='http://www.google.com/', founder=u'\u5f20\u671d\u9633')
Website(name='Sina', url='http://www.sina.com.cn/', founder=u'\u738b\u5fd7\u4e1c')
Website(name='163', url='http://www.163.com/', founder=u'\u4e01\u78ca')
struct.unpack

opencv 計算包絡面積

凸包:在數學中,在實向量空間V中的一組點X的凸包或凸包絡是包含X的最小凸集。來自Wikipedia。通俗的來說就是包圍一組散點的最小凸邊形。

在scipy.spatial 和opencv 分別有計算凸包的函式,
scipy中convexHull輸入的引數可以是m*2的點座標。
在opencv 中,cv2.convexHull可以得到凸包的座標值/凸包在輪廓的索引值(取決於引數 returnPoints = True / Fasle)。

cv2.convexhull
cv2.contourArea

長路徑名

python中如果路徑名過長,python程式會找不到檔案;
解決辦法:

def transfer_filepath(path):
	return \\\\?\\os.absolutepath(path)