Python - 如何使用列表推导来填充命名的元组值

时间:2017-05-02 12:57:01

标签: python list collections namedtuple

我有以下课程,可以创建一副牌:

import collections


Card = collections.namedtuple('Card', ['rank', 'suit', 'value'])


class FrenchDeck:
    ranks = [str(n) for n in range(2, 11)] + list('JQKA')
    suits = 'spades clubs hearts diamonds'.split()
    card_value = [str(n + 1) for n in range(len(ranks))]

    def __init__(self):
        self._cards = [Card(rank, suit, value)
                       for suit in self.suits
                       for rank in self.ranks
                       for value in self.card_value
                       ]

    def __len__(self):
        return len(self._cards)

    def __getitem__(self, position):
        return self._cards[position]

if __name__ == '__main__':
    FrenchDeck()

我已将value值添加到卡片中,以便为每张卡片分配一个值,如下所示:

Card(rank='2', suit='spades', value='1')
Card(rank='2', suit='spades', value='2')
Card(rank='2', suit='spades', value='3')
Card(rank='2', suit='spades', value='4')
Card(rank='2', suit='spades', value='5')
Card(rank='2', suit='spades', value='6')
Card(rank='2', suit='spades', value='7')
Card(rank='2', suit='spades', value='8')
Card(rank='2', suit='spades', value='9')
Card(rank='2', suit='spades', value='10')
Card(rank='2', suit='spades', value='11')
Card(rank='2', suit='spades', value='12')
Card(rank='2', suit='spades', value='13')

每件套装为每个等级创造13件物品。我理解为什么会发生这种情况但是我正在努力为这样的西装中的每个等级增加价值:

Card(rank='2', suit='spades', value='1')
Card(rank='3', suit='spades', value='2')
Card(rank='4', suit='spades', value='3')
Card(rank='5', suit='spades', value='4')
Card(rank='6', suit='spades', value='5')
Card(rank='7', suit='spades', value='6')
Card(rank='8', suit='spades', value='7')
Card(rank='9', suit='spades', value='8')
Card(rank='10', suit='spades', value='9')
Card(rank='J', suit='spades', value='10')
Card(rank='K', suit='spades', value='11')
Card(rank='Q', suit='spades', value='12')
Card(rank='A', suit='spades', value='13')

知道如何实现这个目标吗?

2 个答案:

答案 0 :(得分:3)

你实际上没有像你想象的那样嵌套你的列表迭代。

改变
def __init__(self):
    self._cards = [Card(rank, suit, value)
                   for suit in self.suits
                   for rank in self.ranks
                   for value in self.card_value
                   ]

def __init__(self):
    self._cards = [Card(rank, suit, value)
                   for suit in self.suits
                   for rank, value in zip(self.ranks,\
                                          self.card_value)
                   ]

完成你想要的。请注意,这相当于:

def __init__(self):
    self._cards = []
    for suit in self.suits:
        for rank, value in zip(self.ranks, self.card_value):            
            self._cards+=[Card(rank, suit, value)]

此外,正如chepner所说,或由Jérémy操作,

  

value不是Card的独立属性;它是rank

的函数

这意味着您可以value self.card_value self.suitsrank的每个元素,然后根据value得到相应的ranks工作。

更多详情

关注您的评论/问题

您将ranks = [str(n) for n in range(2, 11)] + list('JQKA') 定义为

ranks

然后,根据card_value,您可以按如下方式定义card_value = [str(n + 1) for n in range(len(ranks))]

card_value

清楚地显示了两个对象之间的依赖关系:列表ranks的每个元素,/与列表s的每个元素的/直接转换/对应。

只是为了免费为了清楚起见,我会在指针的名称self.card_value上添加self.card_values。即\r\n

答案 1 :(得分:1)

而不是循环遍历排名,直接通过索引工作进行访问。

    self._cards = [Card(self.ranks[int(value) - 1], suit, value)
                   for suit in self.suits
                   for value in self.card_value
                   ]