我試圖獲得非常大的數組(len> 1000000)的數據切片(基於數組值)。例子請參閱下一個Python代碼我想要在純Python做:numpy中是否有函數比較類似numpy.where函數的多維數組?
vector=[1,2,3,4,5,6,7,8,9,10]
start=[1,4,9] # start and end lists have the same length
end=[2,7,9]
output=[[]]*len(start)
for indx1 in range(len(start)):
temp=[]
for indx2 in range(len(vector)):
if ((vector[indx2]>=start[indx1]) and (vector[indx2]<=end[indx1])):
temp.append(vector[indx2])
output[indx1]=temp
print output
矢量列表中有正常25E + 6的元素,同時開始和結束的名單有像1E6的元素,這就是爲什麼在純粹做這個python非常慢。
你知道爲什麼要用numpy避免for循環來解決這個問題嗎?
感謝您的時間
將'vector'排序?如果不是,將它分類會受傷,還是需要保持原始順序? – 2012-03-16 23:07:20
'start'和'end'定義的間隔是否被排序並且不重疊? – 2012-03-16 23:10:25
矢量的順序必須保持。 – Memolo 2012-03-16 23:23:05