我不斷創建一個隨機生成的列表,大小爲10的New_X
,基於500列。查看列表列表的有效方法?
每次我創建一個新的列表,它必須是唯一的,我的功能NewList
只返回New_X
一旦它尚未被創建並附加到List_Of_Xs
def NewList(Old_List):
end = True
while end == True:
""" Here is code that generates my new sorted list, it is a combination of elements
from Old_List and the other remaining columns,
but the details aren't necessary for this question. """
end = (List_Of_Xs == np.array([New_X])).all(axis=1).any()
List_Of_Xs.append(New_X)
return New_X
我的問題是,是行end = (List_Of_Xs == np.array([New_X])).all(axis=1).any()
尋找List_Of_Xs
有效的方法?
我的List_Of_Xs
可以增長到超過100,000個列表的長度,所以我不確定這是否是低效的。
任何幫助,將不勝感激!
那麼,List_Of_Xs是每個列表有10個元素的列表列表嗎?這些元素是整數嗎?這些整數有上限和下限嗎? – Divakar
我會使'New_X'成爲一個元組,並檢查它是否在Set_of_Xs中。特別是對於10個元素的小列表,應該比進行這種數組比較更快。 – hpaulj
'List_of_Xs == np.array([New_X])'不僅使'New_X'成爲一個數組,而且它每次都爲'List_of_Xs'這樣做。從列表中創建數組並不是一項時間細小的任務。 – hpaulj