获取两个列表的笛卡尔积作为列表列表

Getting the cartesian product of two lists as a list-of-lists

我是 Python 的初学者。

我一直在努力为我的牌组创建一个正确的列表理解。我想在一个列表中创建四个列表,其中每个“数字索引”都有合适的类型。我会尝试在下面展示。

这就是我想要的:

deck = [["1 Hearts, "2H", ..."13H"], ["1 Diamonds", "2D", ..."13D"], [....], [...]]

这是我的:

value = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"]
deck_types = ["hearts", "diamonds", "clubs", "spades"]
deck = [[i] + value for i in deck_types]

输出:

我不能post图片,因为我的声誉低于10,但我会描述它。

[["hearts", "1", "2", "3"....], ["Diamonds", "1", "2", "3"....], ["Clubs", "1"...], [....]]

如您所见,这并不是我想要的。每种类型成为每个子列表中的 nr [0]。我的计划是将 .pop() 函数与 random.randint 结合起来用于抽牌。这样当第一位玩家抽到一张牌时,该牌将从牌组中移除。那么第二个玩家将无法抽到同一张牌。当我打印第一张球员卡时,我希望能够同时看到类型和号码。到目前为止我只能看到这个数字。

像这样:

card1 = deck[random.randint(1, 3)].pop(random.randint(2, 13))
card2 = deck[random.randint(1, 3)].pop(random.randint(2, 13))

所以,我不认为你可以在一个列表理解中做到这一点,因为它需要 2 个循环。这是解构后的版本。

values = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"]
deck = []
card_suits = ["hearts", "diamonds", "clubs", "spades"]
for suit in card_suits:
    suit_deck = []
    for number in values:
        card = f"{number} {suit}"
        suit_deck.append(card)
    deck.append(suit_deck)
    
print(deck)
[['1 hearts', '2 hearts', '3 hearts', '4 hearts', '5 hearts', '6 hearts', '7 hearts', '8 hearts', '9 hearts', '10 hearts', '11 hearts', '12 hearts', '13 hearts'], ['1 diamonds', '2 diamonds', '3 diamonds', '4 diamonds', '5 diamonds', '6 diamonds', '7 diamonds', '8 diamonds', '9 diamonds', '10 diamonds', '11 diamonds', '12 diamonds', '13 diamonds'], ['1 clubs', '2 clubs', '3 clubs', '4 clubs', '5 clubs', '6 clubs', '7 clubs', '8 clubs', '9 clubs', '10 clubs', '11 clubs', '12 clubs', '13 clubs'], ['1 spades', '2 spades', '3 spades', '4 spades', '5 spades', '6 spades', '7 spades', '8 spades', '9 spades', '10 spades', '11 spades', '12 spades', '13 spades']]

或者对每个 suit_deck

使用列表理解
values = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"]
deck = []
card_suits = ["hearts", "diamonds", "clubs", "spades"]
for suit in card_suits:
    suit_deck = [f"{number} {suit}" for number in values]
    deck.append(suit_deck)
    
print(deck)
    

嗯,你只需要多一层列表。

deck = [[x+y for x in value] for y in deck_types]
values = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"]
card_suits = ["hearts", "diamonds", "clubs", "spades"]
[[f"{number} {suit}" for number in values] for suit in card_suits]

[['1 hearts',
  '2 hearts',
  '3 hearts',
  '4 hearts',
  '5 hearts',
  '6 hearts',
  '7 hearts',
  '8 hearts',
  '9 hearts',
  '10 hearts',
  '11 hearts',
  '12 hearts',
  '13 hearts'],
 ['1 diamonds',
  '2 diamonds',
  '3 diamonds',
  '4 diamonds',
  '5 diamonds',
  '6 diamonds',
  '7 diamonds',
  '8 diamonds',
  '9 diamonds',
  '10 diamonds',
  '11 diamonds',
  '12 diamonds',
  '13 diamonds'],
 ['1 clubs',
  '2 clubs',
  '3 clubs',
  '4 clubs',
  '5 clubs',
  '6 clubs',
  '7 clubs',
  '8 clubs',
  '9 clubs',
  '10 clubs',
  '11 clubs',
  '12 clubs',
  '13 clubs'],
 ['1 spades',
  '2 spades',
  '3 spades',
  '4 spades',
  '5 spades',
  '6 spades',
  '7 spades',
  '8 spades',
  '9 spades',
  '10 spades',
  '11 spades',
  '12 spades',
  '13 spades']]