为什么此for循环不在列表中循环?

时间:2019-11-24 05:28:04

标签: python loops

我正在尝试循环创建一副纸牌。我知道这不是一种有效的方法,但是无论如何我都想尝试。由于某种原因,for循环不会像应有的那样在“ Suits”列表中循环。有什么想法吗?

#Pre Assigned Variables
suits = ["Hearts", "Diamonds", "Clubs", "Spades"]
cards = ["Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Ace", "King", "Queen", "Jack"]
deck = []
counter = 0 

#Loop to create deck list
for i in suits:
    while counter != 13:
        card = str(cards[counter]), "of", str(i)
        deck.append(card)
        counter += 1

当我打印卡片组列表时,将显示以下内容:

[('Two', 'of', 'Hearts'), ('Three', 'of', 'Hearts'), ('Four', 'of', 'Hearts'), ('Five', 'of', 'Hearts'), ('Six', 'of', 'Hearts'), ('Seven', 'of', 'Hearts'), ('Eight', 'of', 'Hearts'), ('Nine', 'of', 'Hearts'), ('Ten', 'of', 'Hearts'), ('Ace', 'of', 'Hearts'), ('King', 'of', 'Hearts'), ('Queen', 'of', 'Hearts'), ('Jack', 'of', 'Hearts')]

4 个答案:

答案 0 :(得分:1)

问题在于,当您的计数器在“心”的第一个循环中达到13时,它永远不会重置,这就是为什么它不会在其他花色之间循环的原因。 因此,您可以使用另一个循环来遍历卡,而不必使用计数器。

for i in suits:
    for j in cards:
        card = j, "of", i
        deck.append(card)

答案 1 :(得分:0)

您忘记在每次循环前重设counter变量。 试试这个:

#Pre Assigned Variables
suits = ["Hearts", "Diamonds", "Clubs", "Spades"]
cards = ["Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Ace", "King", "Queen", "Jack"]
deck = []


#Loop to create deck list
for i in suits:
    counter = 0
    while counter != 13:
        card = str(cards[counter]), "of", str(i)
        deck.append(card)
        counter += 1

答案 2 :(得分:0)

您可以为此使用itertools.product

import itertools as it
suits = ["Hearts", "Diamonds", "Clubs", "Spades"]
cards = ["Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Ace", "King", "Queen", "Jack"]
deck = []
#Loop to create deck list
for suit,card in it.product(suits,cards):
    deck.append((card,"of",suit))
print(deck)

输出

[('Two', 'of', 'Hearts'), ('Three', 'of', 'Hearts'), ('Four', 'of', 'Hearts'), ('Five', 'of', 'Hearts'), ('Six', 'of', 'Hearts'), ('Seven', 'of', 'Hearts'), ('Eight', 'of', 'Hearts'), ('Nine', 'of', 'Hearts'), ('Ten', 'of', 'Hearts'), ('Ace', 'of', 'Hearts'), ('King', 'of', 'Hearts'), ('Queen', 'of', 'Hearts'), ('Jack', 'of', 'Hearts'), ('Two', 'of', 'Diamonds'), ('Three', 'of', 'Diamonds'), ('Four', 'of', 'Diamonds'), ('Five', 'of', 'Diamonds'), ('Six', 'of', 'Diamonds'), ('Seven', 'of', 'Diamonds'), ('Eight', 'of', 'Diamonds'), ('Nine', 'of', 'Diamonds'), ('Ten', 'of', 'Diamonds'), ('Ace', 'of', 'Diamonds'), ('King', 'of', 'Diamonds'), ('Queen', 'of', 'Diamonds'), ('Jack', 'of', 'Diamonds'), ('Two', 'of', 'Clubs'), ('Three', 'of', 'Clubs'), ('Four', 'of', 'Clubs'), ('Five', 'of', 'Clubs'), ('Six', 'of', 'Clubs'), ('Seven', 'of', 'Clubs'), ('Eight', 'of', 'Clubs'), ('Nine', 'of', 'Clubs'), ('Ten', 'of', 'Clubs'), ('Ace', 'of', 'Clubs'), ('King', 'of', 'Clubs'), ('Queen', 'of', 'Clubs'), ('Jack', 'of', 'Clubs'), ('Two', 'of', 'Spades'), ('Three', 'of', 'Spades'), ('Four', 'of', 'Spades'), ('Five', 'of', 'Spades'), ('Six', 'of', 'Spades'), ('Seven', 'of', 'Spades'), ('Eight', 'of', 'Spades'), ('Nine', 'of', 'Spades'), ('Ten', 'of', 'Spades'), ('Ace', 'of', 'Spades'), ('King', 'of', 'Spades'), ('Queen', 'of', 'Spades'), ('Jack', 'of', 'Spades')]

答案 3 :(得分:0)

您可能希望每个索引都具有与卡片及其花色相对应的完整字符串(而不是字符串元组)。

添加到Xilin的答案中,完整代码如下:

#Pre Assigned Variables
suits = ["Hearts", "Diamonds", "Clubs", "Spades"]
cards = ["Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Ace", "King", "Queen", "Jack"]
deck = []

for i in suits:
    for j in cards:
        name = j + " of " + i # Note the space prior to and after the word 'of'
        deck.append(name)

您可能还习惯了格式化字符串(非常有用):

for i in suits:
  for j in cards:
    name = "{card} of {suit}".format(card=j, suit=i) 
    deck.append(name)

或者简单地:

for i in suits:
  for j in cards:
    name = "{} of {}".format(j, i) 
    deck.append(name)

有关格式的更多详细信息,可以在这里找到: https://www.geeksforgeeks.org/python-format-function/

这将产生输出:

['Two of Hearts', 'Three of Hearts', 'Four of Hearts', 'Five of Hearts', 'Six of Hearts', 'Seven of Hearts', 'Eight of Hearts', 'Nine of Hearts', 'Ten of Hearts', 'Ace of Hearts', 'King of Hearts', 'Queen of Hearts', 'Jack of Hearts', 'Two of Diamonds', 'Three of Diamonds', 'Four of Diamonds', 'Five of Diamonds', 'Six of Diamonds', 'Seven of Diamonds', 'Eight of Diamonds', 'Nine of Diamonds', 'Ten of Diamonds', 'Ace of Diamonds', 'King of Diamonds', 'Queen of Diamonds', 'Jack of Diamonds', 'Two of Clubs', 'Three of Clubs', 'Four of Clubs', 'Five of Clubs', 'Six of Clubs', 'Seven of Clubs', 'Eight of Clubs', 'Nine of Clubs', 'Ten of Clubs', 'Ace of Clubs', 'King of Clubs', 'Queen of Clubs', 'Jack of Clubs', 'Two of Spades', 'Three of Spades', 'Four of Spades', 'Five of Spades', 'Six of Spades', 'Seven of Spades', 'Eight of Spades', 'Nine of Spades', 'Ten of Spades', 'Ace of Spades', 'King of Spades', 'Queen of Spades', 'Jack of Spades']

您可以使用以下哪一种方法:print(deck)