2006年04月21日 星期五 18:32
不好意思,有一个问题: 在LIST M= [[1,1,1,1], [0,1,1,1], [1,1,0,1], [1,1,1,1]] 里,如果把这四行四列 看成是在 空间的16个点, 我想计算每一个 点到最近的0的距离组成一个 LIST N,比如说 M[0][0]的最近的0 是在M[1][0],所以 N[0][0]=1; M[0][1]的最近的0 也是在M[1][0],所以N[0][1]=2(走了两格); 但是N[0][3]=3因为它离M[2][2]的0更近; N[1][0]=0因为是其本身,距离为0; N应该等于: [[1,2,2,3], [0,1,1,2], [1,1,0,1], [2,2,1,2]] 但是实在想不出来该怎么写代码. 多谢了.
2006年04月21日 星期五 22:36
我貌似搞定了 M= [[1,1,1,1], [0,1,1,1], [1,1,0,1], [1,1,1,1]] Size=len(M) def find(pos,l): x,y = pos for d in range(0,l+1): x1,y1 = x+d,y-l+d if x1>=0 and x1=0 and y1 =0 and x1 =0 and y1 =0 and x1 =0 and y1 =0 and x1 =0 and y1 <samrobertsmith at gmail.com> wrote: > > 不好意思,有一个问题: > 在LIST M= > [[1,1,1,1], > [0,1,1,1], > [1,1,0,1], > [1,1,1,1]] > 里,如果把这四行四列 > 看成是在 > 空间的16个点, > 我想计算每一个 > 点到最近的0的距离组成一个 > LIST N,比如说 M[0][0]的最近的0 > 是在M[1][0],所以 N[0][0]=1; M[0][1]的最近的0 > 也是在M[1][0],所以N[0][1]=2(走了两格); 但是N[0][3]=3因为它离M[2][2]的0更近; > N[1][0]=0因为是其本身,距离为0; > N应该等于: > [[1,2,2,3], > [0,1,1,2], > [1,1,0,1], > [2,2,1,2]] > 但是实在想不出来该怎么写代码. > 多谢了. > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -- http://codeplayer.blogbus.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060421/c272e840/attachment.html
2006年04月21日 星期五 22:44
上面那个是碰巧,应该是这样的: M= [[1,1,1,1], [0,1,1,1], [1,1,0,1], [1,1,1,1]] Size=len(M) def find(pos,l): x,y = pos for d in range(0,l+1): x1,y1 = x+d,y-l+d if x1>=0 and x1=0 and y1 =0 and x1 =0 and y1 =0 and x1 =0 and y1 =0 and x1 =0 and y1 <yi.codeplayer at gmail.com> wrote: > > 我貌似搞定了 > > > M= [[1,1,1,1], > [0,1,1,1], > [1,1,0,1], > [1,1,1,1]] > Size=len(M) > > def find(pos,l): > x,y = pos > for d in range(0,l+1): > x1,y1 = x+d,y-l+d > if x1>=0 and x1 =0 and y1 > return True > x1,y1 = x-d,y+l-d > if x1>=0 and x1 =0 and y1 > return True > else: > if l%2==0: > x1,y1 = x+d/2,y+d/2 > if x1>=0 and x1 =0 and y1 > return True > x1,y1 = x-d/2,y-d/2 > if x1>=0 and x1 =0 and y1 > return True > else: > return False > > def distance(x,y): > for l in range(Size): > if find((x,y),l): > return l > else: > print x,y,l > return -1 > > if __name__=='__main__': > N = [[distance(x,y) for y,i in enumerate(row)] for x,row in > enumerate(M)] > print N > #print find((3,3),2) > > > > On 4/21/06, linda. s <samrobertsmith at gmail.com> wrote: > > > 不好意思,有一个问题: > 在LIST M= > [[1,1,1,1], > [0,1,1,1], > [1,1,0,1], > [1,1,1,1]] > 里,如果把这四行四列 > 看成是在 > 空间的16个点, > 我想计算每一个 > 点到最近的0的距离组成一个 > LIST N,比如说 M[0][0]的最近的0 > 是在M[1][0],所以 N[0][0]=1; M[0][1]的最近的0 > 也是在M[1][0],所以N[0][1]=2(走了两格); 但是N[0][3]=3因为它离M[2][2]的0更近; > N[1][0]=0因为是其本身,距离为0; > N应该等于: > [[1,2,2,3], > [0,1,1,2], > [1,1,0,1], > [2,2,1,2]] > 但是实在想不出来该怎么写代码. > 多谢了. > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > -- > http://codeplayer.blogbus.com/ > -- http://codeplayer.blogbus.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060421/3b8c64c8/attachment.htm
2006年04月21日 星期五 22:51
你这个对 n * n 好像没问题,n * m and m != n就不对了。比如 M= [[1,1,1,1,1,1], [0,1,1,1,1,1], [1,1,0,1,1,1], [1,1,0,1,1,1], [1,1,1,1,1,1]] 2006/4/21, yi huang <yi.codeplayer at gmail.com>: > 上面那个是碰巧,应该是这样的: > > > M= [[1,1,1,1], > [0,1,1,1], > [1,1,0,1], > [1,1,1,1]] > Size=len(M) > > def find(pos,l): > x,y = pos > for d in range(0,l+1): > x1,y1 = x+d,y-l+d > if x1>=0 and x1=0 and y1 > return True > x1,y1 = x-d,y+l-d > if x1>=0 and x1 =0 and y1 > return True > x1,y1 = x+d,y+l-d > if x1>=0 and x1 =0 and y1 > return True > x1,y1 = x-d,y-l+d > if x1>=0 and x1 =0 and y1 > return True > else: > return False > > def distance(x,y): > for l in range(Size): > if find((x,y),l): > return l > else: > print x,y,l > return -1 > > if __name__=='__main__': > N = [[distance(x,y) for y,i in enumerate(row)] for x,row in > enumerate(M)] > print N > #print find((3,3),2) > > > > > On 4/21/06, yi huang <yi.codeplayer at gmail.com> wrote: > > > > 我貌似搞定了 > > > > > > M= [[1,1,1,1], > > [0,1,1,1], > > [1,1,0,1], > > [1,1,1,1]] > > > > Size=len(M) > > > > def find(pos,l): > > x,y = pos > > for d in range(0,l+1): > > x1,y1 = x+d,y-l+d > > if x1>=0 and x1 =0 and y1 > > return True > > x1,y1 = x-d,y+l-d > > if x1>=0 and x1 =0 and y1 > > return True > > else: > > if l%2==0: > > x1,y1 = x+d/2,y+d/2 > > if x1>=0 and x1 =0 and y1 > > return True > > x1,y1 = x-d/2,y-d/2 > > if x1>=0 and x1 =0 and y1 > > return True > > else: > > return False > > > > def distance(x,y): > > for l in range(Size): > > if find((x,y),l): > > return l > > else: > > print x,y,l > > return -1 > > > > if __name__=='__main__': > > N = [[distance(x,y) for y,i in enumerate(row)] for x,row in > enumerate(M)] > > print N > > #print find((3,3),2) > > > > > > > > > > > > On 4/21/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > > > > > > 不好意思,有一个问题: > > 在LIST M= > > [[1,1,1,1], > > [0,1,1,1], > > [1,1,0,1], > > [1,1,1,1]] > > 里,如果把这四行四列 > > 看成是在 > > 空间的16个点, > > 我想计算每一个 > > 点到最近的0的距离组成一个 > > LIST N,比如说 M[0][0]的最近的0 > > 是在M[1][0],所以 N[0][0]=1; M[0][1]的最近的0 > > 也是在M[1][0],所以N[0][1]=2(走了两格); 但是N[0][3]=3因为它离M[2][2]的0更近; > > N[1][0]=0因为是其本身,距离为0; > > N应该等于: > > [[1,2,2,3], > > [0,1,1,2], > > [1,1,0,1], > > [2,2,1,2]] > > 但是实在想不出来该怎么写代码. > > 多谢了. > > > > > > _______________________________________________ > > python-chinese > > Post: send python-chinese at lists.python.cn > > Subscribe: send subscribe to > python-chinese-request at lists.python.cn > > Unsubscribe: send unsubscribe to > python-chinese-request at lists.python.cn > > Detail Info: > http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > -- > > http://codeplayer.blogbus.com/ > > > > -- > http://codeplayer.blogbus.com/ > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to > python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to > python-chinese-request at lists.python.cn > Detail Info: > http://python.cn/mailman/listinfo/python-chinese > >
2006年04月21日 星期五 23:07
m!=n 只要稍微改改就可以了 On 4/21/06, tocer <tocer.deng at gmail.com> wrote: > > 你这个对 n * n 好像没问题,n * m and m != n就不对了。比如 > M= [[1,1,1,1,1,1], > [0,1,1,1,1,1], > [1,1,0,1,1,1], > [1,1,0,1,1,1], > [1,1,1,1,1,1]] > 2006/4/21, yi huang <yi.codeplayer at gmail.com>: > > 上面那个是碰巧,应该是这样的: > > > > > > M= [[1,1,1,1], > > [0,1,1,1], > > [1,1,0,1], > > [1,1,1,1]] > > Size=len(M) > > > > def find(pos,l): > > x,y = pos > > for d in range(0,l+1): > > x1,y1 = x+d,y-l+d > > if x1>=0 and x1=0 and y1 > > return True > > x1,y1 = x-d,y+l-d > > if x1>=0 and x1 =0 and y1 > > return True > > x1,y1 = x+d,y+l-d > > if x1>=0 and x1 =0 and y1 > > return True > > x1,y1 = x-d,y-l+d > > if x1>=0 and x1 =0 and y1 > > return True > > else: > > return False > > > > def distance(x,y): > > for l in range(Size): > > if find((x,y),l): > > return l > > else: > > print x,y,l > > return -1 > > > > if __name__=='__main__': > > N = [[distance(x,y) for y,i in enumerate(row)] for x,row in > > enumerate(M)] > > print N > > #print find((3,3),2) > > > > > > > > > > On 4/21/06, yi huang <yi.codeplayer at gmail.com> wrote: > > > > > > 我貌似搞定了 > > > > > > > > > M= [[1,1,1,1], > > > [0,1,1,1], > > > [1,1,0,1], > > > [1,1,1,1]] > > > > > > Size=len(M) > > > > > > def find(pos,l): > > > x,y = pos > > > for d in range(0,l+1): > > > x1,y1 = x+d,y-l+d > > > if x1>=0 and x1 =0 and y1 > > > return True > > > x1,y1 = x-d,y+l-d > > > if x1>=0 and x1 =0 and y1 > > > return True > > > else: > > > if l%2==0: > > > x1,y1 = x+d/2,y+d/2 > > > if x1>=0 and x1 =0 and y1 > M[x1][y1]==0: > > > return True > > > x1,y1 = x-d/2,y-d/2 > > > if x1>=0 and x1 =0 and y1 > M[x1][y1]==0: > > > return True > > > else: > > > return False > > > > > > def distance(x,y): > > > for l in range(Size): > > > if find((x,y),l): > > > return l > > > else: > > > print x,y,l > > > return -1 > > > > > > if __name__=='__main__': > > > N = [[distance(x,y) for y,i in enumerate(row)] for x,row in > > enumerate(M)] > > > print N > > > #print find((3,3),2) > > > > > > > > > > > > > > > > > > On 4/21/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > > > > > > > > > > 不好意思,有一个问题: > > > 在LIST M= > > > [[1,1,1,1], > > > [0,1,1,1], > > > [1,1,0,1], > > > [1,1,1,1]] > > > 里,如果把这四行四列 > > > 看成是在 > > > 空间的16个点, > > > 我想计算每一个 > > > 点到最近的0的距离组成一个 > > > LIST N,比如说 M[0][0]的最近的0 > > > 是在M[1][0],所以 N[0][0]=1; M[0][1]的最近的0 > > > 也是在M[1][0],所以N[0][1]=2(走了两格); 但是N[0][3]=3因为它离M[2][2]的0更近; > > > N[1][0]=0因为是其本身,距离为0; > > > N应该等于: > > > [[1,2,2,3], > > > [0,1,1,2], > > > [1,1,0,1], > > > [2,2,1,2]] > > > 但是实在想不出来该怎么写代码. > > > 多谢了. > > > > > > > > > _______________________________________________ > > > python-chinese > > > Post: send python-chinese at lists.python.cn > > > Subscribe: send subscribe to > > python-chinese-request at lists.python.cn > > > Unsubscribe: send unsubscribe to > > python-chinese-request at lists.python.cn > > > Detail Info: > > http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > > > > > -- > > > http://codeplayer.blogbus.com/ > > > > > > > > -- > > http://codeplayer.blogbus.com/ > > _______________________________________________ > > python-chinese > > Post: send python-chinese at lists.python.cn > > Subscribe: send subscribe to > > python-chinese-request at lists.python.cn > > Unsubscribe: send unsubscribe to > > python-chinese-request at lists.python.cn > > Detail Info: > > http://python.cn/mailman/listinfo/python-chinese > > > > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -- http://codeplayer.blogbus.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060421/963fb37f/attachment.html
2006年04月21日 星期五 23:11
M= [[1,1,1,1,1,1], [0,1,1,1,1,1], [1,1,0,1,1,1], [1,1,0,1,1,1], [1,1,1,1,1,1]] # X * Y X=len(M) Y=len(M[0]) if X>Y: Big=X else:Big=Y def find(pos,l): x,y = pos for d in range(0,l+1): x1,y1 = x+d,y-l+d if x1>=0 and x1=0 and y1 =0 and x1 =0 and y1 =0 and x1 =0 and y1 =0 and x1 =0 and y1 <yi.codeplayer at gmail.com> wrote: > > m!=n 只要稍微改改就可以了 > > > On 4/21/06, tocer <tocer.deng at gmail.com> wrote: > > > > 你这个对 n * n 好像没问题,n * m and m != n就不对了。比如 > > M= [[1,1,1,1,1,1], > > [0,1,1,1,1,1], > > [1,1,0,1,1,1], > > [1,1,0,1,1,1], > > [1,1,1,1,1,1]] > > 2006/4/21, yi huang < yi.codeplayer at gmail.com>: > > > 上面那个是碰巧,应该是这样的: > > > > > > > > > M= [[1,1,1,1], > > > [0,1,1,1], > > > [1,1,0,1], > > > [1,1,1,1]] > > > Size=len(M) > > > > > > def find(pos,l): > > > x,y = pos > > > for d in range(0,l+1): > > > x1,y1 = x+d,y-l+d > > > if x1>=0 and x1 =0 and y1 > > > return True > > > x1,y1 = x-d,y+l-d > > > if x1>=0 and x1 =0 and y1 > > > return True > > > x1,y1 = x+d,y+l-d > > > if x1>=0 and x1 =0 and y1 > > > return True > > > x1,y1 = x-d,y-l+d > > > if x1>=0 and x1 =0 and y1 > > > return True > > > else: > > > return False > > > > > > def distance(x,y): > > > for l in range(Size): > > > if find((x,y),l): > > > return l > > > else: > > > print x,y,l > > > return -1 > > > > > > if __name__=='__main__': > > > N = [[distance(x,y) for y,i in enumerate(row)] for x,row in > > > enumerate(M)] > > > print N > > > #print find((3,3),2) > > > > > > > > > > > > > > > On 4/21/06, yi huang < yi.codeplayer at gmail.com> wrote: > > > > > > > > 我貌似搞定了 > > > > > > > > > > > > M= [[1,1,1,1], > > > > [0,1,1,1], > > > > [1,1,0,1], > > > > [1,1,1,1]] > > > > > > > > Size=len(M) > > > > > > > > def find(pos,l): > > > > x,y = pos > > > > for d in range(0,l+1): > > > > x1,y1 = x+d,y-l+d > > > > if x1>=0 and x1 =0 and y1 > > > > > > return True > > > > x1,y1 = x-d,y+l-d > > > > if x1>=0 and x1 =0 and y1 > > > > return True > > > > else: > > > > if l%2==0: > > > > x1,y1 = x+d/2,y+d/2 > > > > if x1>=0 and x1 =0 and y1 > > M[x1][y1]==0: > > > > return True > > > > x1,y1 = x-d/2,y-d/2 > > > > if x1>=0 and x1 =0 and y1 > > M[x1][y1]==0: > > > > return True > > > > else: > > > > return False > > > > > > > > def distance(x,y): > > > > for l in range(Size): > > > > if find((x,y),l): > > > > return l > > > > else: > > > > print x,y,l > > > > return -1 > > > > > > > > if __name__=='__main__': > > > > N = [[distance(x,y) for y,i in enumerate(row)] for x,row in > > > enumerate(M)] > > > > print N > > > > #print find((3,3),2) > > > > > > > > > > > > > > > > > > > > > > > > On 4/21/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > > > > > > > > > > > > > > 不好意思,有一个问题: > > > > 在LIST M= > > > > [[1,1,1,1], > > > > [0,1,1,1], > > > > [1,1,0,1], > > > > [1,1,1,1]] > > > > 里,如果把这四行四列 > > > > 看成是在 > > > > 空间的16个点, > > > > 我想计算每一个 > > > > 点到最近的0的距离组成一个 > > > > LIST N,比如说 M[0][0]的最近的0 > > > > 是在M[1][0],所以 N[0][0]=1; M[0][1]的最近的0 > > > > 也是在M[1][0],所以N[0][1]=2(走了两格); 但是N[0][3]=3因为它离M[2][2]的0更近; > > > > N[1][0]=0因为是其本身,距离为0; > > > > N应该等于: > > > > [[1,2,2,3], > > > > [0,1,1,2], > > > > [1,1,0,1], > > > > [2,2,1,2]] > > > > 但是实在想不出来该怎么写代码. > > > > 多谢了. > > > > > > > > > > > > _______________________________________________ > > > > python-chinese > > > > Post: send python-chinese at lists.python.cn > > > > Subscribe: send subscribe to > > > python-chinese-request at lists.python.cn > > > > Unsubscribe: send unsubscribe to > > > python-chinese-request at lists.python.cn > > > > Detail Info: > > > http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > > > > > > > > > > -- > > > > http://codeplayer.blogbus.com/ > > > > > > > > > > > > -- > > > http://codeplayer.blogbus.com/ > > > _______________________________________________ > > > python-chinese > > > Post: send python-chinese at lists.python.cn > > > Subscribe: send subscribe to > > > python-chinese-request at lists.python.cn > > > Unsubscribe: send unsubscribe to > > > python-chinese-request at lists.python.cn > > > Detail Info: > > > http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > _______________________________________________ > > python-chinese > > Post: send python-chinese at lists.python.cn > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > > -- > http://codeplayer.blogbus.com/ > -- http://codeplayer.blogbus.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060421/880ad528/attachment.htm
2006年04月21日 星期五 23:16
恩,真不错 2006/4/21, yi huang <yi.codeplayer at gmail.com>: > M= [[1,1,1,1,1,1], > [0,1,1,1,1,1], > [1,1,0,1,1,1], > [1,1,0,1,1,1], > [1,1,1,1,1,1]] > # X * Y > X=len(M) > Y=len(M[0]) > if X>Y: > Big=X > else:Big=Y > > > def find(pos,l): > x,y = pos > for d in range(0,l+1): > x1,y1 = x+d,y-l+d > if x1>=0 and x1=0 and y1 > > return True > x1,y1 = x-d,y+l-d > if x1>=0 and x1=0 and y1 > > return True > x1,y1 = x+d,y+l-d > if x1>=0 and x1 =0 and y1 > > return True > x1,y1 = x-d,y-l+d > if x1>=0 and x1 =0 and y1 > > return True > else: > return False > > def distance(x,y): > for l in range(Big): > > if find((x,y),l): > return l > else: > print x,y,l > return -1 > > if __name__=='__main__': > N = [[distance(x,y) for y,i in enumerate(row)] for x,row in > enumerate(M)] > print N > #print find((3,3),2) > > > > On 4/21/06, yi huang <yi.codeplayer at gmail.com> wrote: > > > > m!=n 只要稍微改改就可以了 > > > > > > > > On 4/21/06, tocer < tocer.deng at gmail.com> wrote: > > > 你这个对 n * n 好像没问题,n * m and m != n就不对了。比如 > > > M= [[1,1,1,1,1,1], > > > [0,1,1,1,1,1], > > > [1,1,0,1,1,1], > > > [1,1,0,1,1,1], > > > [1,1,1,1,1,1]] > > > 2006/4/21, yi huang < yi.codeplayer at gmail.com>: > > > > 上面那个是碰巧,应该是这样的: > > > > > > > > > > > > M= [[1,1,1,1], > > > > [0,1,1,1], > > > > [1,1,0,1], > > > > [1,1,1,1]] > > > > Size=len(M) > > > > > > > > def find(pos,l): > > > > x,y = pos > > > > for d in range(0,l+1): > > > > x1,y1 = x+d,y-l+d > > > > if x1>=0 and x1 =0 and y1 > > > > return True > > > > x1,y1 = x-d,y+l-d > > > > if x1>=0 and x1 =0 and y1 > > > > return True > > > > x1,y1 = x+d,y+l-d > > > > if x1>=0 and x1 =0 and y1 > > > > return True > > > > x1,y1 = x-d,y-l+d > > > > if x1>=0 and x1 =0 and y1 > > > > return True > > > > else: > > > > return False > > > > > > > > def distance(x,y): > > > > for l in range(Size): > > > > if find((x,y),l): > > > > return l > > > > else: > > > > print x,y,l > > > > return -1 > > > > > > > > if __name__=='__main__': > > > > N = [[distance(x,y) for y,i in enumerate(row)] for x,row in > > > > enumerate(M)] > > > > print N > > > > #print find((3,3),2) > > > > > > > > > > > > > > > > > > > > On 4/21/06, yi huang < yi.codeplayer at gmail.com> wrote: > > > > > > > > > > 我貌似搞定了 > > > > > > > > > > > > > > > M= [[1,1,1,1], > > > > > [0,1,1,1], > > > > > [1,1,0,1], > > > > > [1,1,1,1]] > > > > > > > > > > Size=len(M) > > > > > > > > > > def find(pos,l): > > > > > x,y = pos > > > > > for d in range(0,l+1): > > > > > x1,y1 = x+d,y-l+d > > > > > if x1>=0 and x1 =0 and y1 > > > > > return True > > > > > x1,y1 = x-d,y+l-d > > > > > if x1>=0 and x1 =0 and y1 > > > > > return True > > > > > else: > > > > > if l%2==0: > > > > > x1,y1 = x+d/2,y+d/2 > > > > > if x1>=0 and x1 =0 and y1 > M[x1][y1]==0: > > > > > return True > > > > > x1,y1 = x-d/2,y-d/2 > > > > > if x1>=0 and x1 =0 and y1 > M[x1][y1]==0: > > > > > return True > > > > > else: > > > > > return False > > > > > > > > > > def distance(x,y): > > > > > for l in range(Size): > > > > > if find((x,y),l): > > > > > return l > > > > > else: > > > > > print x,y,l > > > > > return -1 > > > > > > > > > > if __name__=='__main__': > > > > > N = [[distance(x,y) for y,i in enumerate(row)] for x,row in > > > > enumerate(M)] > > > > > print N > > > > > #print find((3,3),2) > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > On 4/21/06, linda. s <samrobertsmith at gmail.com > wrote: > > > > > > > > > > > > > > > > > > > > > 不好意思,有一个问题: > > > > > 在LIST M= > > > > > [[1,1,1,1], > > > > > [0,1,1,1], > > > > > [1,1,0,1], > > > > > [1,1,1,1]] > > > > > 里,如果把这四行四列 > > > > > 看成是在 > > > > > 空间的16个点, > > > > > 我想计算每一个 > > > > > 点到最近的0的距离组成一个 > > > > > LIST N,比如说 M[0][0]的最近的0 > > > > > 是在M[1][0],所以 N[0][0]=1; M[0][1]的最近的0 > > > > > 也是在M[1][0],所以N[0][1]=2(走了两格); 但是N[0][3]=3因为它离M[2][2]的0更近; > > > > > N[1][0]=0因为是其本身,距离为0; > > > > > N应该等于: > > > > > [[1,2,2,3], > > > > > [0,1,1,2], > > > > > [1,1,0,1], > > > > > [2,2,1,2]] > > > > > 但是实在想不出来该怎么写代码. > > > > > 多谢了. > > > > > > > > > > > > > > > _______________________________________________ > > > > > python-chinese > > > > > Post: send python-chinese at lists.python.cn > > > > > Subscribe: send subscribe to > > > > python-chinese-request at lists.python.cn > > > > > Unsubscribe: send unsubscribe to > > > > python-chinese-request at lists.python.cn > > > > > Detail Info: > > > > http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > > > > > > > > > > > > > > > -- > > > > > http://codeplayer.blogbus.com/ > > > > > > > > > > > > > > > > -- > > > > http://codeplayer.blogbus.com/ > > > > _______________________________________________ > > > > python-chinese > > > > Post: send python-chinese at lists.python.cn > > > > Subscribe: send subscribe to > > > > python-chinese-request at lists.python.cn > > > > Unsubscribe: send unsubscribe to > > > > python-chinese-request at lists.python.cn > > > > Detail Info: > > > > http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > > > > _______________________________________________ > > > python-chinese > > > Post: send python-chinese at lists.python.cn > > > Subscribe: send subscribe to > python-chinese-request at lists.python.cn > > > Unsubscribe: send unsubscribe to > python-chinese-request at lists.python.cn > > > Detail Info: > http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > > > > -- > > http://codeplayer.blogbus.com/ > > > > -- > http://codeplayer.blogbus.com/ > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to > python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to > python-chinese-request at lists.python.cn > Detail Info: > http://python.cn/mailman/listinfo/python-chinese > >
2006年04月22日 星期六 10:29
还是有点问题,比如: M= [[1,1,1,1,1,1], [1,1,1,1,1,1], [1,1,1,1,1,1], [1,1,1,1,1,1], [0,1,1,1,1,1]] 我也写了一个: # -*- coding=cp936 -*- """author: tocer""" from sys import maxint def distance(M): zeroList = [(x,y) for y,rows in enumerate(M) for x,item in enumerate(rows) if item == 0 ] #下面这一段应该还可以优化 for y,rows in enumerate(M[:]): for x,item in enumerate(rows): if item == 1: distance = maxint for m,n in zeroList: distance = min(distance, abs(x-m) + abs(y-n)) M[y][x] = distance if __name__ == '__main__': M= [[1,1,1,1,1,1], [1,1,1,1,1,1], [1,1,1,1,1,1], [1,1,1,1,1,1], [0,1,1,1,1,1]] distance(M) for rows in M: print rows 在 06-4-21,tocer<tocer.deng at gmail.com> 写道: > 恩,真不错 > > 2006/4/21, yi huang <yi.codeplayer at gmail.com>: > > M= [[1,1,1,1,1,1], > > [0,1,1,1,1,1], > > [1,1,0,1,1,1], > > [1,1,0,1,1,1], > > [1,1,1,1,1,1]] > > # X * Y > > X=len(M) > > Y=len(M[0]) > > if X>Y: > > Big=X > > else:Big=Y > > > > > > def find(pos,l): > > x,y = pos > > for d in range(0,l+1): > > x1,y1 = x+d,y-l+d > > if x1>=0 and x1=0 and y1 > > > > return True > > x1,y1 = x-d,y+l-d > > if x1>=0 and x1=0 and y1 > > > > return True > > x1,y1 = x+d,y+l-d > > if x1>=0 and x1 =0 and y1 > > > > return True > > x1,y1 = x-d,y-l+d > > if x1>=0 and x1 =0 and y1 > > > > return True > > else: > > return False > > > > def distance(x,y): > > for l in range(Big): > > > > if find((x,y),l): > > return l > > else: > > print x,y,l > > return -1 > > > > if __name__=='__main__': > > N = [[distance(x,y) for y,i in enumerate(row)] for x,row in > > enumerate(M)] > > print N > > #print find((3,3),2) > > > > > > > > On 4/21/06, yi huang <yi.codeplayer at gmail.com> wrote: > > > > > > m!=n 只要稍微改改就可以了 > > > > > > > > > > > > On 4/21/06, tocer < tocer.deng at gmail.com> wrote: > > > > 你这个对 n * n 好像没问题,n * m and m != n就不对了。比如 > > > > M= [[1,1,1,1,1,1], > > > > [0,1,1,1,1,1], > > > > [1,1,0,1,1,1], > > > > [1,1,0,1,1,1], > > > > [1,1,1,1,1,1]] > > > > 2006/4/21, yi huang < yi.codeplayer at gmail.com>: > > > > > 上面那个是碰巧,应该是这样的: > > > > > > > > > > > > > > > M= [[1,1,1,1], > > > > > [0,1,1,1], > > > > > [1,1,0,1], > > > > > [1,1,1,1]] > > > > > Size=len(M) > > > > > > > > > > def find(pos,l): > > > > > x,y = pos > > > > > for d in range(0,l+1): > > > > > x1,y1 = x+d,y-l+d > > > > > if x1>=0 and x1 =0 and y1 > > > > > return True > > > > > x1,y1 = x-d,y+l-d > > > > > if x1>=0 and x1 =0 and y1 > > > > > return True > > > > > x1,y1 = x+d,y+l-d > > > > > if x1>=0 and x1 =0 and y1 > > > > > return True > > > > > x1,y1 = x-d,y-l+d > > > > > if x1>=0 and x1 =0 and y1 > > > > > return True > > > > > else: > > > > > return False > > > > > > > > > > def distance(x,y): > > > > > for l in range(Size): > > > > > if find((x,y),l): > > > > > return l > > > > > else: > > > > > print x,y,l > > > > > return -1 > > > > > > > > > > if __name__=='__main__': > > > > > N = [[distance(x,y) for y,i in enumerate(row)] for x,row in > > > > > enumerate(M)] > > > > > print N > > > > > #print find((3,3),2) > > > > > > > > > > > > > > > > > > > > > > > > > On 4/21/06, yi huang < yi.codeplayer at gmail.com> wrote: > > > > > > > > > > > > 我貌似搞定了 > > > > > > > > > > > > > > > > > > M= [[1,1,1,1], > > > > > > [0,1,1,1], > > > > > > [1,1,0,1], > > > > > > [1,1,1,1]] > > > > > > > > > > > > Size=len(M) > > > > > > > > > > > > def find(pos,l): > > > > > > x,y = pos > > > > > > for d in range(0,l+1): > > > > > > x1,y1 = x+d,y-l+d > > > > > > if x1>=0 and x1 =0 and y1 > > > > > > return True > > > > > > x1,y1 = x-d,y+l-d > > > > > > if x1>=0 and x1 =0 and y1 > > > > > > return True > > > > > > else: > > > > > > if l%2==0: > > > > > > x1,y1 = x+d/2,y+d/2 > > > > > > if x1>=0 and x1 =0 and y1 > > M[x1][y1]==0: > > > > > > return True > > > > > > x1,y1 = x-d/2,y-d/2 > > > > > > if x1>=0 and x1 =0 and y1 > > M[x1][y1]==0: > > > > > > return True > > > > > > else: > > > > > > return False > > > > > > > > > > > > def distance(x,y): > > > > > > for l in range(Size): > > > > > > if find((x,y),l): > > > > > > return l > > > > > > else: > > > > > > print x,y,l > > > > > > return -1 > > > > > > > > > > > > if __name__=='__main__': > > > > > > N = [[distance(x,y) for y,i in enumerate(row)] for x,row in > > > > > enumerate(M)] > > > > > > print N > > > > > > #print find((3,3),2) > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > On 4/21/06, linda. s <samrobertsmith at gmail.com > wrote: > > > > > > > > > > > > > > > > > > > > > > > > > 不好意思,有一个问题: > > > > > > 在LIST M= > > > > > > [[1,1,1,1], > > > > > > [0,1,1,1], > > > > > > [1,1,0,1], > > > > > > [1,1,1,1]] > > > > > > 里,如果把这四行四列 > > > > > > 看成是在 > > > > > > 空间的16个点, > > > > > > 我想计算每一个 > > > > > > 点到最近的0的距离组成一个 > > > > > > LIST N,比如说 M[0][0]的最近的0 > > > > > > 是在M[1][0],所以 N[0][0]=1; M[0][1]的最近的0 > > > > > > 也是在M[1][0],所以N[0][1]=2(走了两格); 但是N[0][3]=3因为它离M[2][2]的0更近; > > > > > > N[1][0]=0因为是其本身,距离为0; > > > > > > N应该等于: > > > > > > [[1,2,2,3], > > > > > > [0,1,1,2], > > > > > > [1,1,0,1], > > > > > > [2,2,1,2]] > > > > > > 但是实在想不出来该怎么写代码. > > > > > > 多谢了. > > > > > > > > > > > > > > > > > > _______________________________________________ > > > > > > python-chinese > > > > > > Post: send python-chinese at lists.python.cn > > > > > > Subscribe: send subscribe to > > > > > python-chinese-request at lists.python.cn > > > > > > Unsubscribe: send unsubscribe to > > > > > python-chinese-request at lists.python.cn > > > > > > Detail Info: > > > > > http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > -- > > > > > > http://codeplayer.blogbus.com/ > > > > > > > > > > > > > > > > > > > > -- > > > > > http://codeplayer.blogbus.com/ > > > > > _______________________________________________ > > > > > python-chinese > > > > > Post: send python-chinese at lists.python.cn > > > > > Subscribe: send subscribe to > > > > > python-chinese-request at lists.python.cn > > > > > Unsubscribe: send unsubscribe to > > > > > python-chinese-request at lists.python.cn > > > > > Detail Info: > > > > > http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > > > > > > > > _______________________________________________ > > > > python-chinese > > > > Post: send python-chinese at lists.python.cn > > > > Subscribe: send subscribe to > > python-chinese-request at lists.python.cn > > > > Unsubscribe: send unsubscribe to > > python-chinese-request at lists.python.cn > > > > Detail Info: > > http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > > > > > > > > > > -- > > > http://codeplayer.blogbus.com/ > > > > > > > > -- > > http://codeplayer.blogbus.com/ > > _______________________________________________ > > python-chinese > > Post: send python-chinese at lists.python.cn > > Subscribe: send subscribe to > > python-chinese-request at lists.python.cn > > Unsubscribe: send unsubscribe to > > python-chinese-request at lists.python.cn > > Detail Info: > > http://python.cn/mailman/listinfo/python-chinese > > > > >
Zeuux © 2025
京ICP备05028076号