Hi, Linda, > -----Original Message----- > I have a question. in the LIST M= > [[1,1,1,1], > [0,1,1,1], > [1,1,0,1], > [1,1,1,1]] > If treat them as the locations of 16 points, I want to generate another > list > N to hold the corresponding value for each point to its nearest 0. > For example: > the nearest 0 point to M[0][0] is M[1][0], so N[0][0]=1; > M[0][1]'s nearest 0 is also at M[1][0], so N[0][1]=2; > but N[0][3]=3 because 0 at M[2][2] is much nearer; > N[1][0]=0 because the distance to itself is 0; > N should be > [[1,2,2,3], > [0,1,1,2], > [1,1,0,1], > [2,2,1,2]] > I am really very new to Python and have no idea how to write the code. > Thanks! > Linda
Basically, what you need to do is compare the coordinates of each cell in N with the coordinates of each cell in M WHICH CONTAINS A 0. Here is my solution: 1. create the list N a. same structure as M b. set each cell to a huge integer value (will be replaced later) 2. make a third list, C, containing the coordinates of the 0 cells in M 3. walk the list N a. compare each cell's coordinates with each coordinate pair in C 1. calculate the distance between the two coordinate pairs b. assign the smallest value obtained from 3.a. to this cell in N. Here are some code fragments to get you started. To, create the coordinate list (C), walk the list M, looking for 0s: >>>>>>> C = [] for i in xrange(len(M)): for j in xrange(len(M[i])): if M[i][j] == 0: C.append((i,j)) >>>>>>> (This could probably be rewritten as a list comprehension, but I'm not very good at them yet.) =8^( To find the distance between the coordinate pairs: >>>>>>> dist = abs(C[k][0] - i) + abs(C[k][1] - j) >>>>>>> To check for a minimum distance: >>>>>>> if dist < N[i][j]: N[i][j] = dist >>>>>>> Does this give you enough information to write your solution? If not, just post more questions. Regards, Barry [EMAIL PROTECTED] 541-302-1107 ________________________ We who cut mere stones must always be envisioning cathedrals. -Quarry worker's creed _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor