Python论坛  - 讨论区

标题:[python-chinese] 距离

2006年04月21日 星期五 18:32

linda.s samrobertsmith at gmail.com
Fri Apr 21 18:32:57 HKT 2006

不好意思,有一个问题:
在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]]
但是实在想不出来该怎么写代码.
多谢了.

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年04月21日 星期五 22:36

yi huang yi.codeplayer at gmail.com
Fri Apr 21 22:36:11 HKT 2006

我貌似搞定了

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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年04月21日 星期五 22:44

yi huang yi.codeplayer at gmail.com
Fri Apr 21 22:44:08 HKT 2006

上面那个是碰巧,应该是这样的:

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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年04月21日 星期五 22:51

tocer tocer.deng at gmail.com
Fri Apr 21 22:51:45 HKT 2006

你这个对 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
>
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年04月21日 星期五 23:07

yi huang yi.codeplayer at gmail.com
Fri Apr 21 23:07:00 HKT 2006

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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年04月21日 星期五 23:11

yi huang yi.codeplayer at gmail.com
Fri Apr 21 23:11:43 HKT 2006

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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年04月21日 星期五 23:16

tocer tocer.deng at gmail.com
Fri Apr 21 23:16:07 HKT 2006

恩,真不错

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
>
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年04月22日 星期六 10:29

tocer tocer.deng at gmail.com
Sat Apr 22 10:29:08 HKT 2006

还是有点问题,比如:
 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
> >
> >
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号