我正在持续创建一个基于500列的大小为10的随机生成列表
每次我创建一个新列表时,它必须是独一无二的,并且我的函数
New_X
。每次我创建一个新列表时,它必须是独一无二的,并且我的函数
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个元素的列表吗?这些元素是整数吗?这些整数有上下限吗? - DivakarNew_X
设为元组,并检查它是否在集合Set_of_Xs
中。尤其是对于仅由10个元素组成的小列表,这样做比进行数组比较更快。 - hpauljList_of_Xs==np.array([New_X])
不仅将New_X
转换为数组,而且每次都会对List_of_Xs
进行转换。从列表的列表中创建数组不是一个时间上不重要的任务。 - hpaulj