Python检查对象成员值是否在列表中

时间:2017-01-26 04:07:38

标签: python list

我有一个名为Point的课程:

class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __eq__(self, other):
        return self.x == other.x and self.y == other.y

我有一个包含Point个对象的列表,我调用一个方法来检查它是否在列表中,如下所示:

def isInList(self, list, point):
    for cell in list:
        if(cell == point):
            return True

    return False

然而,它永远不会进入if语句;所以它总是返回false。我知道一个事实,由于我在程序中显示的可视化,匹配点必须在列表中。我做错了什么?

我将这两种方法称为Point列表。我这样通过了cellsdirections(多次使用不同的点和方向调用):

point = Point(2, 4)
direction = Direction.UP
newList = self.setCells(point, direction)

def setCells(self, point, direction):
    pointList = []
    index = 0
    done = False

    while index < 20 and done == False:
        newPoint = self.getNextCell(point, direction)

        if(point not in pointList):
            pointList.append(newPoint)
            point = pointList[len(pointList)-1]
            index += 1
        else:
            done = True

    return pointList

def getNextCell(self, point, direction):
    if(direction == Direction.UP):
        return Point(point.x-1, point.y, Direction.UP)
    elif(direction == Direction.DOWN):
        return Point(point.x+1, point.y, Direction.DOWN)
    elif(direction == Direction.LEFT):
        return Point(point.x, point.y-1, Direction.LEFT)
    elif(direction == Direction.RIGHT):
        return Point(point.x, point.y+1, Direction.RIGHT)

Direction是一个枚举:

class Direction(Enum):
    NONE = 1
    UP = 2
    DOWN = 3
    LEFT = 4
    RIGHT = 5

2 个答案:

答案 0 :(得分:1)

不幸的是,来自OP问题的代码似乎包含了要绑定到对象(-webkit-transform-origin: bottom left; transform-origin: bottom left; -webkit-transform: rotateX( 30deg ); transform: rotateX( 30deg ); background: red; -webkit-transform-style: preserve-3d; transform-style: preserve-3d; setCells)的函数,但是在没有它们的类的情况下展示。此外,对getNextCellPoint的构造函数的调用还有一个额外的参数(方向),我将其删除。因此,我正在考虑以下代码:

导入枚举

getNextCells

打印False。要调查这是否正确,向类class Point(object): def __init__(self, x, y): self.x = x self.y = y def __eq__(self, other): return self.x == other.x and self.y == other.y def setCells(point, direction): pointList = [] index = 0 done = False while index < 20 and done == False: newPoint = getNextCell(point, direction) if(point not in pointList): pointList.append(newPoint) point = pointList[len(pointList)-1] index += 1 else: done = True return pointList class Direction(enum.Enum): NONE = 1 UP = 2 DOWN = 3 LEFT = 4 RIGHT = 5 def getNextCell(point, direction): if(direction == Direction.UP): return Point(point.x-1, point.y) elif(direction == Direction.DOWN): return Point(point.x+1, point.y) elif(direction == Direction.LEFT): return Point(point.x, point.y-1) elif(direction == Direction.RIGHT): return Point(point.x, point.y+1) point = Point(2, 4) direction = Direction.UP newList = setCells(point, direction) print(point in newList) 添加一些漂亮的打印很有用:

Point

现在可以轻松调查列表的内容:

def __repr__(self):
    return "Point(%s,%s)" % (self.x,self.y)

由于>>> print(newList) [Point(1,4)] >>> print(point) Point(2,4) 只包含一个具有不同x坐标的点,因此很明显newList应为false。

为了更好地理解为什么point in newList不包含newList,提供setCells的简化版本会很有用:

point

说明:最初,pointList为空,因此无论def setCellsSimplified(point,direction): return [getNextCell(point, direction)] 的值如何,point not in pointList都将为真。因此point已添加到newPointpointList现在等于point。因此,当while-body再次运行时,newPoint位于point中,第二次执行是while-body的最后一次执行。

由于pointList始终返回一个新的getNextCell,它在一个坐标上与传递给它的对象不同Pointpoint永远不相等(无论方向如何)和因此,getNextCell(point,dir)始终为假(通过查看简化版本point in setCells(point,dir)可以轻松查看)。

答案 1 :(得分:0)

我很困惑你如何调用它。您的函数包含self,list和point。如果这是一个对象的方法,并且你想看看当前对象是否在列表中,我会尝试这样的东西......

    def isInList(self, list):
        return self in list

那应该给你一个与此类似的输出......

>>> list = [Point(2,2), Point(3,3), Point(4,4)]
>>> x = Point(4,4)
>>> x.isInList(list)
True
>>> x = Point(4,5)
>>> x.isInList(list)
False