【问题标题】:How to make a deck of cards with several variables?如何制作具有多个变量的一副牌?
【发布时间】:2018-09-26 10:57:35
【问题描述】:

所以,我要做一副不正常的纸牌。每张卡片都有颜色(红色、绿色、蓝色)、度数(1,2,3)、符号(三角形、正方形、圆形)和数字。(1,2,3)我有一个看起来像这样。

class card:
def __init__(self, color, degree, symbol, number):
    self.color=color
    self.degree=degree
    self.symbol=symbol
    self.number=number
def __repr__(self):
    return "(%s,%s,%s,%s)" %(self.color,self.degree,self.symbol,self.number)

我也有这些列表,其中包含所有变量和我想要卡片的一副卡片。

colors=["red", "green", "blue"]
degrees=["1","2","3"]
symbols=["triangle", "square", "circle"]
numbers=["1","2","3"]
deck=[]

现在,我想做的是用所有可能的卡片创建一个完整的套牌。最好它们是随机顺序的,但不是必需的。 我知道如果它只是一个数字和颜色,我可以很容易地做到这一点。

deck = [card(value, color) for value in range(0, 2) for color in colors]

但是,当我还要使用符号和度数时,我不知道如何制作它。我试图建立更多的 if 语句来循环它,但这没有用。我也不希望同一张卡片出现两次,也不想要一张不遵守班级规则的卡片,它们必须被结构化为[颜色,度数,符号,数字]

有人知道该去哪里吗?

【问题讨论】:

    标签: python playing-cards


    【解决方案1】:

    所有可能的卡片组合的完整套牌:

    deck = [card(color, degree, symbol, number) for color in colors \
            for degree in degrees for symbol in symbols for number in numbers]
    

    要随机化牌组中的卡片顺序,请查看:Shuffling a list of objects

    【讨论】:

      【解决方案2】:

      itertools使用product

      import itertools
      
      
      deck = [
          card(color, degree, symbol, number)
          for color, degree, symbol, number in
          itertools.product(colors, degrees, symbols, numbers)
      ]
      

      【讨论】:

        【解决方案3】:

        您想要颜色、度数、符号和数字的所有组合吗?

        如果是这样,请使用嵌套的 for 循环:

        deck = []
        for color in colors:
            for degree in degrees:
                for symbol in symbols:
                    for number in numbers:
                        deck.append(card(color, degree, symbol, number)
        
        # Also written as a list comprehension
        deck = [
            card(color, degree, symbol, number)
            for color in colors
                for degree in degrees
                    for symbol in symbols
                        for number in numbers
        ]  # The indent is just to show how it works. For style, put them all at the same indent.
        

        或者使用itertools.product(也可以偷懒)

        deck = itertools.starmap(card, itertools.product(colors, degrees, symbols, numbers))
        
        deck = list(deck)  # If you really need it to be a list
        

        【讨论】:

        • 我在那里尝试了第一个循环,但它只给了我 29 张牌,而我希望整个牌组包含 81 张。为什么没有更多?我无法让第二个示例在那里工作,因为我使用了一个主函数,该函数调用了这个制作甲板的函数,然后它无法识别甲板对象,当我尝试使用 itertools 时同样的问题。
        【解决方案4】:
        import itertools
        
        identifiers = [colors, degrees, symbols, numbers]
        deck = [[*i] for i in itertools.product(*identifiers)]
        
        [['red', '1', 'triangle', '1'], ['red', '1', 'triangle', '2'], ['red', '1', 'triangle', '3'],...
        

        【讨论】:

          猜你喜欢
          • 2013-12-30
          • 1970-01-01
          • 2021-08-31
          • 2020-09-30
          • 2021-06-12
          • 1970-01-01
          • 2014-09-03
          • 2022-11-25
          • 1970-01-01
          相关资源
          最近更新 更多