Python - 如何使用列表理解来填充命名元组值

Python - How to use list comprehension to populate a named tuples values

我有以下 class 创建一副纸牌:

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')

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

您实际上并没有像您认为的那样嵌套列表迭代。

改变自

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 is not an independent attribute of a Card; it is a function of the rank.

这意味着你可以对self.suits的每个元素遍历self.card_valuevalue,然后根据[=15=得到对应的rank ] 你继续努力。

更多详情

关注您的comment/question

您将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列表的每个元素,is/has一个直接transformation/correspondance of/with列表的每个元素ranks.

免费 为了清楚起见,我会在指针的名称 self.card_value 后加一个 s。即 self.card_values

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

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