2

私はPythonを学んでいます(VBAのバックグラウンドを持っています)。

この辞書が読み込まれないのはなぜですか?私はカードの完全なデッキを考え出そうとしています。

これが私のコードです:

class Deck:
    def load_deck(self):
        suite = ('Spades', 'Hearts', 'Diamonds', 'Clubs')
        rank = (2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King", "Ace")
        full_deck={}
        for s in suite:
            for r in rank:
                full_deck.setdefault(s,r)
        return full_deck
raw_deck = Deck()
raw_deck1 = raw_deck.load_deck()
print raw_deck1

これが私の出力です:

{'Hearts': 2, 'Clubs': 2, 'Spades': 2, 'Diamonds': 2}
4

4 に答える 4

3

JBernardoのコメントは、の正しい使用法を示してsetdefault()いますが、ループを次のように簡略化できます。

full_deck = {}
for s in suite:
    full_deck[s] = rank

または、タプルの代わりにリストが必要な場合は、を使用しますlist(rank)

一発ギャグ:

full_deck = {s: rank for s in suite}

Python 2.6以下:

full_deck = dict((s, rank) for s in suite)
于 2012-08-04T00:11:20.403 に答える
1

作成しているメソッドは、実際にはクラスに含まれている必要はありません。実行しようとしていることによっては...ディクショナリにデータを入力しようとしているだけの場合は、setdefault(を使用する代わりに値を割り当てることができます。 )メソッド(上記のとおり)。

あなたはこれと同じくらい簡単にそれをすることができます:

cards = {}
for suit in ('Heart', 'Club', 'Spade', 'Diamond'): 
    cards[suit] = range(2, 11) + ['Jack', 'Queen', 'King', 'Ace']
print cards

If you are trying to populate a new class type with information, then you should define an init function (which acts as the class constructor in Python) to create and store new member variables, i.e.:

class Deck:
    def __init__( self ):
        self._cards = {}
        for suit in ('Heart', 'Club', 'Spade', 'Diamond'):
            self._cards[suit] = range(2, 11) + ['Jack', 'Queen', 'King', 'Ace']

    def cards( self ):
        return self._cards

deck = Deck()
print deck.cards()
于 2012-08-04T00:18:10.960 に答える
0

Using the response from @ernie pointing out that I need one dictionary with a single key for 52 individual pairs, I came up with the following and it does what I wanted.

def load_deck():
    suite = ('Spades', 'Hearts', 'Diamonds', 'Clubs')
    rank = ('2', '3', '4', '5', '6', '7', '8', '9', '10', "Jack", "Queen", "King", "Ace")
    full_deck = {}
    i = 0
    for s in suite:
        for r in rank:
            full_deck[i] = r,s  # took this solution from the comment below.  it works.
            i += 1
    return full_deck
print load_deck()
于 2012-08-04T02:24:56.277 に答える
0

Here's an efficient way to do it in Python 2.7 which supports dictionary comprehensions:

def load_deck():
    suite = ('Spades', 'Hearts', 'Diamonds', 'Clubs')
    rank = ('2', '3', '4', '5', '6', '7', '8', '9', '10', "Jack", "Queen", "King", "Ace")
    return {item[0]:item[1] for item in enumerate(((r,s) for r in rank for s in suite))}

For Python < 2.7 you'd need to use another generator expression with the dict class constructor like this:

def load_deck():
    suite = ('Spades', 'Hearts', 'Diamonds', 'Clubs')
    rank = ('2', '3', '4', '5', '6', '7', '8', '9', '10', "Jack", "Queen", "King", "Ace")
    return dict((item[0],item[1])
                for item in enumerate(((r,s) for r in rank for s in suite)))
于 2012-08-04T03:38:35.210 に答える