- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这是我第一次使用 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/
当我在我的类中实现 __cmp__ 函数时,python 是否会在内部重载“==”,我们在 C++ 中是如何做到的? 只是好奇。我是 python 的新手。 :) 最佳答案 ==的含义当您定义 __c
我创建了一个新的 Python 对象,如下所示 class Mylist(list): def __cmp__(self,other): if len(self)>len(ot
如果您不为方法 __cmp__ 和 __str__ 定义自己的方法会怎样? 最佳答案 If no __cmp__(), __eq__() or __ne__() operation is define
Python 2.x 有两种重载比较运算符的方法, __cmp__ 或“丰富的比较运算符”,例如 __lt__ . 富比较重载据说是首选,但为什么会这样呢? 丰富的比较运算符实现起来更简单,但您必须使
您好,我正在覆盖 __cmp__ 。如果传递的第二个对象是 None,或者它不是 someClass 的实例,则返回 -1。 我不明白这里到底发生了什么。 class someClass():
如何更改实例(不在类中)的 __cmp__ 函数? 例如: class foo: def __init__(self, num): self.num = num def cmp
这是我第一次使用 stack overflow,所以如果格式与网站不太相符,我深表歉意。我最近才开始学习编程,已经快两周了。我正在从 http://openbookproject.net/thinkc
我想编写一个可用作可哈希集合中的键的类(例如,在 dict 中)。我知道用户类在默认情况下是可哈希的,但是在这里使用 id(self) 是错误的。 我的类持有一个元组作为成员变量。从 tuple 派生
class x: def __init__(self,name): self.name=name def __str__(self): return s
如果我在 python 2.7.10 中这样做 import Queue class Item(object): def __init__(self): self.name =
我想覆盖 __cmp__、__eq__ 和 __hash__,这样我就可以在 SQLAlchemy Declarative Base 模型上进行设置操作。这会导致与声明性基础实现有任何冲突吗? 最佳答
在 python 中 docs ( yeah, I have this thing with the docs ) 它说: User-defined classes have __cmp__() an
我有一个intervaltree需要对Intervals进行排序并最终对Points进行排序的库。 我有一个 __cmp__ 方法,它强制执行非常稳定和逻辑的排序(请参阅代码末尾)。而且,我有一个有用
我一直在尝试创建一个继承自 UserDict.DictMixin 的 dict 子类,该子类支持非哈希键。性能不是问题。不幸的是,Python 通过尝试从子类创建 dict 对象来实现 DictMix
下面这段代码 class point: def __init__(self, x, y): self.x = x self.y = y def disp
我是一名优秀的程序员,十分优秀!