简体   繁体   English

基本问题。 制定一个长 for 循环

[英]Basic Question. Working out a long for loop

I want to write a for loop that make six decks of cards into variables but I cannot figure out how to write the code to determine what number the card is on.我想编写一个 for 循环,将六副卡片变成变量,但我不知道如何编写代码来确定卡片的编号。

for i in range(1, 313):
    for j in range(1, 53):
        for k in range(1, 5):
            if k == 1:
                color = "clubs"
            if k == 2:
                color = "diamond"
            if k == 3:
                color = "hearts"
            if k == 4:
                color = "spades"
            for y in range(1, 14):
                print(f"card_{i} = tuple(color = {color}, num = {y})")

It will print out 52 version of the card then go on to the next one instead I want it to write out 6 different deck of cards but all the cards have different variable name.它将打印出卡片的 52 个版本,然后 go 打印到下一张,而不是我希望它写出 6 张不同的卡片组,但所有卡片都有不同的变量名。 Here is my desired output.这是我想要的 output。

card_1 = tuple(color = clubs, num = 1)
#This would then go on till the 13th card then it would change to diamond.
card_12 = tuple(color = clubs, num = 13)
card_13 = tuple(color = diamond, num = 1)
card_14 = tuple(color = diamond, num = 2)

There are several ways to approach this.有几种方法可以解决这个问题。 I am gonna offer a couple of ideas.我将提供几个想法。 The first idea is probably most closely aligned with what you are describing/showing in your own code.第一个想法可能与您在自己的代码中描述/显示的内容最接近。

Approach 1: most similar to your code:方法1:与您的代码最相似:

decks = {}
i = 0
for deck in range(6):
    for suit in ['clubs', 'diamond', 'hearts', 'spades']:
        for card in range(1, 14):
            i += 1
            print(f"card_{i} = tuple(color = {suit}, num = {card})")


card_1 = tuple(color = clubs, num = 1)
card_2 = tuple(color = clubs, num = 2)
...
card_12 = tuple(color = clubs, num = 12)
card_13 = tuple(color = clubs, num = 13)
card_14 = tuple(color = diamond, num = 1)
card_15 = tuple(color = diamond, num = 2)

Approach 2: uses itertools.product() to remove a for loop:方法 2:使用 itertools.product() 删除 for 循环:

A potentially better approach that removes one layer of the nested for loops is to use the itertools.product() function.删除一层嵌套for循环的一种可能更好的方法是使用itertools.product() function。

from itertools import product

i = 0
for deck in range(6):
    for suit, num in product(['clubs', 'diamond', 'hearts', 'spades'], range(1,14)):
        i += 1
        print(f"card_{i} = tuple(color = {suit}, num = {num})")

Approach 3: Better storage of the results方法 3:更好地存储结果

Having said all of that, it is not clear to me whether you want real tuple s stored in memory OR if you simply want to print something that looks like a tuple to the screen.说了这么多,我不清楚您是否想要将真正的tuple存储在 memory 中,或者您只是想在屏幕上打印一些看起来像元组的东西。 Storing the data in a dictionary datatype would allow you to use that data later, if need be.如果需要,将数据存储在dictionary数据类型中将允许您稍后使用该数据。 Presuming that you want real tuple s, the following code will store all the results of your work in some form of datatype, like a dictionary of tuple s.假设您想要真正的tuple ,以下代码将以某种形式的数据类型存储您工作的所有结果,例如tupledictionary

NOTE: tuple s don't really store date using named arguments as you have shown above, they are more like lists where elements are simply stored in them, so below in the code, you will see us storing the element without any sort of named arguments:注意: tuple s 并没有真正使用命名的 arguments 来存储日期,如上所示,它们更像是列表,其中元素只是存储在其中,因此在下面的代码中,您将看到我们存储的元素没有任何类型的命名arguments:

tuple(suit, num) instead of tuple(color=suit, num=num) tuple(suit, num)而不是tuple(color=suit, num=num)

Let's see how this works out.让我们看看这是怎么回事。

from itertools import product

i = 0
decks = {}
for deck in range(6):
    for suit, num in product(['clubs', 'diamond', 'hearts', 'spades'], range(1,14)):
        i += 1

        # NOTE: here we are storing the data in a dictionary
        # NOTE: tuples don't store items using named values
        decks[f"card_{i}"] = tuple((suit, num))


# NOTE: here we use the items() method on our decks dictionary
# to extract each key and value in pairs so we can print them.
for card_num, card in decks.items():
    print(card_num, '=', card)


card_1 = ('clubs', 1)
card_2 = ('clubs', 2)
...
card_12 = ('clubs', 12)
card_13 = ('clubs', 13)
card_14 = ('diamond', 1)
card_15 = ('diamond', 2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM