Python列表重复整个列表的最后一个元素

时间:2015-12-23 16:47:19

标签: python list poker

我正在写一个python程序只是为了制作一个简单的扑克游戏。我只是想熟悉Python语言。我知道我编写python代码的方法并不完全是Pythonic,但实际上我只想弄清楚为什么我的列表会将最后一个对象重复附加到List中的每个空格。在我追加时,列表具有正确的值,但在它之后只重复追加最后一个对象。我花了大约两天时间试图解决这个问题,而且我确定它只是我用语言忽略的东西。任何帮助,将不胜感激。这是代码:

class Card(object):
    suit = ""
    value = ""

    def __init__(self, suit, value):
        Card.suit=suit
        Card.value=value

def createDeck():

    suit = ["DIAMONDS", "HEARTS", "CLUBS","SPADES"]
    value = ["ACE", "TWO", "THREE", "FOUR", "FIVE", 
         "SIX", "SEVEN", "EIGHT", "NINE", "TEN", 
         "JACK", "QUEEN", "KING"]
    Deck = []

    for i in range(4):
        for j in range(13):
            card = Card(suit[i],value[j])
            Deck.append(card)
            print(Deck[i].value, "OF", Deck[i].suit)

    displayDeck(Deck)

def displayDeck(Deck = [], *args):
    for i in range(len(Deck)):
        print(Deck[i].value, " OF ", Deck[i].suit)

这是我得到的输出,为简洁起见缩短了它:     C:\ Users \ root \ Desktop> py Poker.py

ACE OF DIAMONDS

-All Cards Through-

KING OF SPADES

KING OF SPADES

KING OF SPADES

KING OF SPADES

KING OF SPADES
etc. Until The list is filled (52 spots)

4 个答案:

答案 0 :(得分:2)

Card.__init__ - 方法中,您可以设置类属性,而不是实例属性。因此每个Card实例都具有相同的属性,最后一个属性(黑桃王)。因此,使用self.设置实例属性:

class Card(object):
    def __init__(self, suit, value):
        self.suit = suit
        self.value = value

因此,类属性的一种用法是套装和值名称的常量:

class Card(object):
    SUITS = ["DIAMONDS", "HEARTS", "CLUBS","SPADES"]
    VALUES = ["ACE", "TWO", "THREE", "FOUR", "FIVE", 
        "SIX", "SEVEN", "EIGHT", "NINE", "TEN", 
        "JACK", "QUEEN", "KING"]

    def __init__(self, suit, value):
        self.suit = suit
        self.value = value

def create_deck():
    deck = []
    for suit in Card.SUITS:
        for value in Card.VALUES:
            card = Card(suit,value)
            deck.append(card)
            print(card.value, "OF", card.suit)
    return deck

def display_deck(deck):
    for card in deck:
        print(card.value, " OF ", card.suit)

deck = create_deck()
display_deck(deck)

答案 1 :(得分:1)

尝试这样

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div>
  <div class='input'>
    <textarea class='editor' placeholder='write away...'></textarea>
  </div>
  <div class='output'></div>
</div>

这就是你如何正确设置Card对象的套装和价值。

    def __init__(self, suit, value):
       self.suit=suit
       self.value=value

我在循环中删除了print语句。 现在输出是:

for i in range(4):
    for j in range(13):
        card = Card(suit[i],value[j])
        Deck.append(card)
displayDeck(Deck)  

答案 2 :(得分:1)

为了详细说明Rockybilly的答案,您的/** * A class that refers to an existing object instance. This class does not own * the existing object instance and the lifetime of the existing object instance * must be greater than the lifetime of instantiations of this class. */ template<typename T> class Handle { public: Handle(T& object) : mObject(&object) {} // ... other functions necessary to use this object ... private: T* mObject; }; 课程定义了两个 class 属性,Cardsuitvalue类的每个实例都共享这两个值,这意味着如果在一个实例中更改它,则会在所有实例中更改它们。当您执行Card之类的操作时,这就是Card.__init__方法中正在发生的情况。您正在修改您创建的所有 Card.suit = suit的套装。在你的情况下,黑桃王是你创造的最后一张牌,所以它的套装和价值是为你创造的所有52张牌设定的。

您要做的是将Cardsuit视为实例值,这意味着您可以通过value在班级中引用它们。此外,您不需要在类级别设置它们的值(即,在self行的正下方)。相反,您只需在class Card方法中初始化它们。尝试将__init__课程更改为:

Card

答案 3 :(得分:0)

首先,您可以像这样修复Card课程:

class Card(object):
    def __init__(self, suit = None, value = None):
        self.suit=suit
        self.value=value

这将解决您多次显示 last 卡的问题,因为您不再经常修改相同的Card对象。但在修复之后,由于您的打印声明,您可能会遇到另一个问题。这是因为在j的每个循环中,您使用了卡片组中的i卡片,但ij增加13之前没有增加current = 0 for i in range(4): for j in range(13): card = Card(suit[i],value[j]) Deck.append(card) print(Deck[current].value, "OF", Deck[current].suit) current = += 1 }。这会更好:

for i in range(4):
    for j in range(13):
        card = Card(suit[i],value[j])
        Deck.append(card)
display_deck(Deck)

但显然是凌乱的。你想要:

get.vertex.attribute