python列表的加法_Python列表加法理解
我是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",
"ven", "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,"ven":7, "eight":8,"nine":9,
中枢神经受损"ten":10, "jack":10, "queen":10, "king":10}
def __init__(lf, rank=None, suit=None):
"""Constructor."""
if rank not in lf.ranks:不动产
rai ValueError("Invalid rank.")水蜗牛
if suit not in lf.suits:
rai ValueError("Invalid suit.")家书500字
lf.rank = rank
lf.suit = suit
def __str__(lf):第2课堂
"""A string reprentation of the Card."""
return "{0}:{1}".format(lf.rank, lf.suit)
公司简介文案
另⼀个类创建卡⽚对象列表,并定义以下函数:
^{pr2}$
据我所见,value函数是列表理解的候选函数,但我⽆法使其⼯作。我相信下⾯的内容是正确的,但是我仍然会遇到语法错误,我不知道我做错了什么。请注意,我是Python新⼿,并列出理解:def value(lf):
面试通知短信result = [x += y for x = Card.values[y.rank] for y in lf.cards]