在while循环内修改变量?

问题描述

我正在尝试使用 Voronoi 对象为缺陷集群创建一个列表中的列表。我首先创建一个字典,其中键是粒子的索引,值是它们的邻居。然后我创建一个缺陷列表,其中一个缺陷是给定粒子的邻居数!= 6。带有注释的代码如下。我感到困惑的是测试第二个集群的下一步 - 我被指示使用这个 while 循环,但我想不出任何方法来创建一个新的集合,在 while 循环中包含下一个粒子的邻居.有任何想法吗?另外,附带问题,我应该使用“potential_defects = set(neigh[clusters[0][0]])”还是“potential_defects = set(*neigh[clusters[0][0]])”?我不太清楚 * 的作用。

代码

def vor_neighbors(vor):
    """Takes a vornoi diagram from scipy.spatial.Voronoi and returns a dict
    where the keys are particle indices and the values are lists of neighbors"""
    
    #test code is as follows for particle index 6:
    #np.unique(vor.ridge_points[np.any(vor.ridge_points == 6,axis=1)].flatten())
    
    #want an aboject called neigh such that
    #>>> neigh[6]
    #[ 2,9,12,14,18]
    neighbors = {}
    for i in range(vor.points.shape[0]):
         n_array = (np.unique(vor.ridge_points[np.any(vor.ridge_points == i,axis = 1)].flatten()))
         n_array = n_array[n_array != i]
         neighbors[i] = list(n_array)
    return neighbors
    

full = ca.read_xyz("flat.xyz")
simple_mask = np.linalg.norm(full,axis=-1)<4
particles = full[simple_mask][np.newaxis,:]

i = 0
vor = Voronoi(particles[i,:,:2])
de = delaunay(particles[i,:2])
q = ca.charge(de)
    
defects = np.nonzero(q!=0)[0]
    
#the algorithm
#initialize with our first defect cluster
clusters = [[defects[0]]]
    
#keep track of unvisited defects
neigh = vor_neighbors(vor)
potential_defects = set(neigh[clusters[0][0]])

while len(potential_defects) > 0:
    #Goes through every defect at the edge of the cluster
    current = potential_defects.pop()
    
    #check if this particle is a defect
    if q[current]==0:
        #skip this loop
        pass
    elif int(current) in clusters: #check if works on nested lists
        #skip loop,we seen it already
        pass
    else: #not already seen,is a defect,so add to current cluster
        clusters[0].append(current)
        

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)