Python - Insert value to list in a dictionary -


i need fix code. try append value list in dictionary.

def distance(x1, y1, x2, y2):   dis=((x1-x2)**2) + ((y1-y2)**2)   return dis  def cluster_member_formation2(arrch, arrn, k):   dicch = dict.fromkeys(arrch,[])   arre = []   j in range(len(arrch)):     d_nya = distance(arrn[1][0], arrn[1][1], arrn[arrch[j]][0], arrn[arrch[j]][1])     arre.append(d_nya)   minc = min(arre)   ind = arre.index(minc)   x = arrch[ind]   dicch[x].append(1)   print(arre, minc, ind, x, dicch)  arrch=[23, 35] arrn={0:[23, 45, 2, 0], 1:[30,21,2,0], 23:[12, 16, 2, 0], 35:[48, 77, 2, 0]}  cluster_member_formation2(arrch, arrn, 1) 

the output:

[349, 3460] 349 0 23 {35: [1], 23: [1]} 

i try calculate distance between node 1 , node in arrch, , take minimum distance. in output show result of arre [349, 3460], minimum 349. 349 has index 0, find arrch index 0, likes arrch[0]=23. finally, want update dicch[23].append(1) result is

{35: [], 23: [1]} 

but, why code update keys, 35 , 23?

i hope can me. thank you..

classmethod fromkeys(seq[, value])

create new dictionary keys seq , values set value.

all of dictionary values reference same single list instance ([]) provide value fromkeys function.

you use dictionary comprehension seen in this answer.

dicch = {key: [] key in arrch} 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -