我是Python新手,不能将函数转换为列表理解。理解涉及value函数,其包含类如下:class Card(object):
# Lists containing valid candidates for a card's rank and suit.
suits = [None, "spade", "club", "heart", "diamond"]
ranks = [None, "ace", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten", "jack", "queen", "king"]
# Dictionary containing the ranks and their associative values.
values = {None:0, "ace":1, "two":2, "three":3, "four":4,
"five":5,"six":6,"seven":7, "eight":8,"nine":9,
"ten":10, "jack":10, "queen":10, "king":10}
def __init__(self, rank=None, suit=None):
"""Constructor."""
if rank not in self.ranks:
raise ValueError("Invalid rank.")
if suit not in self.suits:
raise ValueError("Invalid suit.")
self.rank = rank
self.suit = suit
def __str__(self):
"""A string representation of the Card."""
return "{0}:{1}".format(self.rank, self.suit)
另一个类创建卡片对象列表,并定义以下函数:
^{pr2}$
据我所见,value函数是列表理解的候选函数,但我无法使其工作。我相信下面的内容是正确的,但是我仍然会遇到语法错误,我不知道我做错了什么。请注意,我是Python新手,并列出理解:def value(self):
result = [x += y for x = Card.values[y.rank] for y in self.cards]