我有一个类,在一个列表中我保存了该类的一些实例。在添加新实例之前,我想检查 x, y 是否与其他项目相同。如果 x,y 已经存在于另一个实例中,我想制作另一个随机对。当我找到一个唯一的对时,我会将新实例附加到列表中。
通过仅使用列表并在 for 循环内检查来检查这一点的最有效方法是什么?
class Vehicle :
def __init__(self, CoordX, CoordY, Z) :
self.X=CoordX
self.Y=CoordY
self.Z=Z
VehicleList=[]
for i in range (1,15+1):
obj_x = randint(1,30)
obj_y = randint (1,20)
obj_z=randint(1,100)
If..#Check if [x,y] of item exists in list and Generate new random
else:
NewVehicle=Vehicle(obj_x,obj_y,obj_z)
VehicleList.append(NewVehicle)