简体   繁体   English

为什么进入循环时我的列表没有变化?

[英]Why my list is not changing when entering the loop?

I'm trying to build a function draw in my Class Circle which gets a matrix which is represented by a list of lists, it has m rows and n columns and it has 0 in every cell, the (i,j) cell represents the (i,j) point, the function should change each (i,j) cell that is contained in the given circle to one. 我正在尝试在我的类圈中构建一个函数绘制,该函数绘制得到一个由列表列表表示的矩阵,它具有m行和n列,并且每个单元格中都有0,(i,j)单元格表示(i,j)点,函数应将给定圆中包含的每个(i,j)单元格更改为一个。

Example: 例:

>>> mat =[[0 for j in range(5)] for i in range(7)] 
>>> mat 
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 
0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] 
>>> Circle(40,10,1).draw(mat) 
>>> mat 
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 1, 1, 1, 
0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] 

This is the code that I wrote: 这是我写的代码:

import math


class Point():
    """ Holds data on a point (x,y) in the plane """

    def __init__(self, x=0, y=0):
        assert isinstance(x,(int, float)) and isinstance(y,(int, float)) 
        self.x = x
        self.y = y




class Circle():
    """ Holds data on a circle in the plane """

    def __init__(self,*args):
        if len(args)==2:
            if isinstance(args[0],Point) and isinstance(args[1],(float,int)):
                assert args[1]>0
                self.center= args[0]
                self.radius= args[1]

        if len(args)==3:
            assert args[2]>0
            self.a=args[0]
            self.b=args[1]
            self.center= Point(self.a,self.b)
            self.radius= args[2]



    def contains(self,check): 

        if isinstance(check,(Point)):
            if math.sqrt((self.center.x-check.x)**2 + (self.center.y-check.y)**2) <= self.radius:
                return True
        if isinstance(check,Circle): 
            test= math.sqrt((self.center.x-check.center.x)**2 + (self.center.x-check.center.x)**2)
            if test < (abs((self.radius)-(check.radius))):
                return True

        else:
            return False

    def intersect(self,other):  
        check= math.sqrt((self.center.x-other.center.x)**2 + (self.center.y-other.center.y)**2)
        if check >(self.radius+other.radius):
            return False
        if check < (self.radius+other.radius):
            return True


    def draw(self,mat):
        for lst in mat:
            for j in lst:
                if Circle.contains(self,(lst,j)):
                    mat[lst[j]]=1

I have find your error! 我发现您的错误! first i add a print in your code, like this: 首先,我在您的代码中添加打印件,如下所示:

def draw(self,mat):
    for lst in mat:
        for j in lst:
            if Circle.contains(self,(lst,j)):
                print 'it is ok'
                mat[lst[j]]=1

run it, you will find that the code print 'it is ok' will never run. 运行它,您将发现代码print 'it is ok'将永远不会运行。 it tell us your if sentence is always false. 它告诉我们您的句子是否总是错误的。

I checked your Circle.contains(self,(lst,j)) function, your param is like ([0,0,...],j), while what your function want is just a Point or Circle, so it will never return True. 我检查了您的Circle.contains(self,(lst,j))函数,您的参数就像([0,0,...],j),而您的函数想要的只是一个Point或Circle,因此它将永不返回True。

I wanna fix your code, but your mat has problems i think, so this is just some suggestion. 我想修复您的代码,但是我认为您的垫子有问题,所以这只是一些建议。 you still need think about your purpose twice! 您仍然需要三思而后行!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM