gpt4 book ai didi

Python: 'object in list' 检查和 '__cmp__' 溢出

转载 作者:太空狗 更新时间:2023-10-29 16:57:59 31 4
gpt4 key购买 nike

这是我第一次使用 stack overflow,所以如果格式与网站不太相符,我深表歉意。我最近才开始学习编程,已经快两周了。我正在从 http://openbookproject.net/thinkcs/python/english3e/index.html 学习 python直到现在一切都很好,我只是被困了几个小时。我在谷歌上搜索了很多,但找不到合适的解决方案,所以我来了。

我正在尝试让 OldMaidGame() 正常运行,如 CH17 中所述。 http://openbookproject.net/thinkcs/python/english3e/ch17.html- 大部分代码也来自上一章。

我发现我无法使 Deck.remove、Hand.remove_matches 或任何其他类型的移除功能起作用。经过一些调试我发现当程序检查给定的卡片是否存在于甲板/手/等中时,就会出现问题。它永远无法匹配。然后经过一些回顾本章(在 ch16 中),我发现“如果卡片在甲板/手/等中:移除(卡片)”等查找对象的 .cmp() 以确定卡片是否实际上存在于甲板/手/等。这是我的 cmp 在对电子书中的给定代码添加“ace”之后的版本。

def __cmp__(self, other):
""" Compares cards, returns 1 if greater, -1 if lesser, 0 if equal """
# check the suits
if self.suit > other.suit: return 1
if self.suit < other.suit: return -1
# suits are the same... check ranks
# check for aces first.
if self.rank == 1 and other.rank == 1: return 0
if self.rank == 1 and other.rank != 1: return 1
if self.rank != 1 and other.rank == 1: return -1
# check for non-aces.
if self.rank > other.rank: return 1
if self.rank < other.rank: return -1
# ranks are the same... it's a tie
return 0

cmp 本身看起来不错,我当然可以使用一些技巧来让它变得更好(比如 ace 检查)。所以我不知道为什么牌组/手牌检查中的牌总是返回 false。这是给定的删除函数:

class Deck:
...
def remove(self, card):
if card in self.cards:
self.cards.remove(card)
return True
else:
return False

拼命想让它工作,我想到了这个:

class Deck:
...
def remove(self, card):
""" Removes the card from the deck, returns true if successful """
for lol in self.cards:
if lol.__cmp__(card) == 0:
self.cards.remove(lol)
return True
return False

似乎工作正常,直到我转向其他不工作的删除功能:

class OldMaidHand(Hand):
def remove_matches(self):
count = 0
original_cards = self.cards[:]
for card in original_cards:
match = Card(3 - card.suit, card.rank)
if match in self.cards:
self.cards.remove(card)
self.cards.remove(match)
print("Hand {0}: {1} matches {2}".format(self.name, card, match))
count = count + 1
return count

我又做了一些调整:

class OldMaidHand(Hand):
def remove_matches(self):
count = 0
original_cards = self.cards[:]
for card in original_cards:
match = Card(3 - card.suit, card.rank)
for lol in self.cards:
if lol.__cmp__(match) == 0:
self.cards.remove(card)
self.cards.remove(match)
print("Hand {0}: {1} matches {2}".format(self.name, card, match))
count = count + 1
return count

移除对卡片来说效果很好,但是当我尝试移除匹配项时它会给出错误(x 不在列表中)。另一个我们左右,我可能也能做到这一点,但因为我已经感觉我走错了路,因为我无法修复原始的“牌组/手牌/等”等,我来这里寻找一些答案/提示。

感谢阅读,非常感谢您提供的任何帮助:)

-------------------- 编辑 1 *

这是我当前的代码: http://pastebin.com/g77Y4Tjr

-------------------- 编辑 2 *

我已经尝试了这里建议的每一个 cmp,但我仍然无法找到带有“in”的卡片。

>>> a = Card(0, 5)
>>> b = Card(0, 1)
>>> c = Card(3, 1)
>>> hand = Hand('Baris')
>>> hand.add(a)
>>> hand.add(b)
>>> hand.add(c)
>>> d = Card(3, 1)
>>> print(hand)
Hand Baris contains
5 of Clubs
Ace of Clubs
Ace of Spades
>>> d in hand.cards
False
>>>

我也尝试过 card.py @DSM 已成功使用,但我也在那里遇到错误,比如在排序函数中它说它无法比较两个卡片对象。
所以我想知道,也许这是 Python 3.2 的问题,或者语法在某处发生了变化?

最佳答案

“所以我想知道,也许这是 Python 3.2 的问题,或者语法在某处发生了变化?”

哦,你运行的是 Python 3.2? 这在 Python 3 中永远行不通:python 3 不使用 __cmp__!

参见 data model (look for __eq__) .另请阅读 what's new in Python 3对于其他一些事情,方式太容易错过了。

抱歉,这是我们 Python 程序员的责任;我们应该早点发现这一点。大多数人可能查看了所有代码,甚至没有考虑就意识到源代码显然是 python 2 代码,并假设这就是我们正在使用的代码。 cmp 函数在 Python 3.2 中甚至不存在,但它没有因 NameError 爆炸的原因是因为从未调用过 __cmp__

如果我在 Python 3.2 中运行代码,我会完全重现您的问题:

>>> c = Card(0,2)
>>> str(c)
'2 of Clubs'
>>> c in [c]
True
>>> c in Deck().cards
False

在 Python 3 中,您可以实现所有丰富的 cmps 或 __eq__ 和其中之一,并使用 total_ordering 装饰器。

from functools import total_ordering

@total_ordering
class Card(object):
"""Represents a standard playing card."""
suit_names = ["Clubs", "Diamonds", "Hearts", "Spades"]
rank_names = [None, "Ace", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King"]
def __init__(self, suit=0, rank=2):
self.suit = suit
self.rank = rank
def __str__(self):
return '%s of %s' % (Card.rank_names[self.rank],
Card.suit_names[self.suit])
def __repr__(self): return str(self)
def __lt__(self, other):
t1 = self.suit, self.rank
t2 = other.suit, other.rank
return t1 < t2
def __eq__(self, other):
t1 = self.suit, self.rank
t2 = other.suit, other.rank
return t1 == t2


>>> c = Card(2,3)
>>> c
3 of Hearts
>>> c in Deck().cards
True

关于Python: 'object in list' 检查和 '__cmp__' 溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6215549/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com