我有一个代码,它基本上测试了盒子触摸中随机分布的圆圈中的哪些圆圈 - 触摸圆圈作为簇添加到字典中。运行此代码时,我不断收到 TypeError:
leftedge = 0
rightedge = 1
result = []
color = {}
parent = {}
clusters = {}
number = 0
def bfs(vertices, neighbours, source) :
global number
number +=1
clusters[number] = set()
color[source] = 'g'
q = []
q.append(source)
while q != [] :
v = q.pop(0)
for v2 in neighbours[v] :
if color[v2] == 'w' :
color[v2] = 'g'
parent[v2] = v
q.append(v2)
color[v] = 'b'
clusters[number].add(v)
def createclusters(vertices, neighbours) :
for v in vertices :
color[v] = 'w'
parent[v] = -1
while 'w' in color.values() :
for v in color.keys() :
if color[v] == 'w' :
bfs(vertices, neighbours, v)
def overlap(c1,c2,r) :
if ((c1[0]-c2[0])**2 +(c1[0]-c2[0])**2)**0.5 > 2*radius :
return 0
return 1
def findclusters(array, radius) :
d={}
for c1 in array :
d[c1]=[]
for c2 in array :
if overlap(c1, c2, radius) :
d[c1].append(c2)
createclusters(array,d)
for cluster in clusters.values() :
l = [i[0] for i in cluster]
left = right = False
x = max(l)
if x + radius > rightedge :
right = True
x = min(l)
if x - radius < leftedge :
left = True
result.append((cluster,left,right))
import numpy.random as nr
array = nr.uniform(size=(10,2)).tolist
radius = 0.1
findclusters(array, radius)
print(clusters)
print(result)
当我尝试运行它时,我收到此错误:
TypeError Traceback (most recent call last)
/Users/annikamonari/<ipython-input-316-be6c65f2ce89> in <module>()
----> 1 findclusters(array,0.1)
/Users/annikamonari/<ipython-input-309-32f214b46080> in findclusters(array, radius)
2 d={}
3 for c1 in array:
----> 4 d[c1]=[]
5 for c2 in array:
6 if overlap(c1,c2,radius):
TypeError: unhashable type: 'list'
对于我的生活,无法弄清楚为什么。任何人都可以弄清楚吗?
谢谢!