Python 自定义类,x在列表中/x不在列表中行为异常
我有一个嵌套的类结构Python 自定义类,x在列表中/x不在列表中行为异常,python,list,contains,equality,Python,List,Contains,Equality,我有一个嵌套的类结构 class Board: def __init__(self, goal=False, skip=False): self.CELLS = [] self.deck = copy.deepcopy(deck) random.shuffle(self.deck) for row in xrange(1,5): for column in xrange(1, 14):
class Board:
def __init__(self, goal=False, skip=False):
self.CELLS = []
self.deck = copy.deepcopy(deck)
random.shuffle(self.deck)
for row in xrange(1,5):
for column in xrange(1, 14):
card = self.deck.pop(0)
cell = Cell(row, column, card)
self.CELLS.append(cell)
def __eq__(self, other):
for cell1, cell2 in zip(self.CELLS, other.CELLS):
if cell1 != cell2:
return False
return True
# return self.CELLS == other.CELLS
def __ne__(self, other):
return not self == other
class Row:
def __init__(self, cells):
self.cells = cells
def __eq__(self, other):
return self.cells == other.cells
def __ne__(self, other):
return not self == other
class Cell:
def __init__(self, row, column, card):
self.row = row
self.column = column
self.index = ROW_COL_TO_INDEX(row, column) # 0 ~ 51
self.card = card
def __eq__(self, other):
if other is not None:
return all([self.row == other.row,
self.column == other.column,
self.index == other.index,
self.card == other.card])
return False
def __ne__(self, other):
return not self == other
class Card:
def __init__(self, value, suite):
self.value = value
self.suite = suite
def __eq__(self, other):
try:
return self.value == other.value and self.suite == other.suite
except AttributeError:
return False
def __ne__(self, other):
return not self == other
deck = []
for suite in ['Hearts', 'Spades', 'Clubs', 'Diamonds']:
for n in xrange(1,14):
if n == 1:
deck.append("SPACE")
else:
deck.append(Card(value=n, suite=suite))
板子有行,行有单元格,单元格有卡片
我正在尝试使用DFS/BFS解决一个纸牌游戏,我正在进行移动,直到找到解决方案,或者发现问题没有解决方案
DFS算法有一个名为“已访问的<代码>列表”
在向堆栈添加新条目之前,我检查该条目是否不在已访问的中。像这样
def depth_first_search(start):
visited, stack = [], [start]
while stack:
move = stack.pop()
if win(move):
return visited
if move not in visited:
visited.append(move)
stack.extend(get_children(move)) # add new entries
raise ValueError("No solution found")
但它仍然会让我重复输入过程,使我无法找到解决方案,或者说没有解决方案
例:
我在类中实现了\uuuuu eq\uuuu
和\uu ne\uuuuu
,但仍然不起作用
发生了什么事?当类定义中几乎肯定存在某种东西时,你不能对你的类定义喋喋不休。你从来没有定义过顶点,它应该是什么?你能给我们看一下你对\uuueq\uuuuu
和\uu ne\uuuuuuuuuuu的定义吗,我在发布时更改了变量名。
Board 1
__ 2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH
__ 2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS
__ 2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC
__ 2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD
.
.
.
Board 315
__ 2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH
__ 2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS
__ 2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC
__ 2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD