0

リスト要素 ( ) を取得し、for ループを使用して別のリスト ( )["3D"]内にネストできる特定の方法があるかどうか疑問に思っていました。[["3D"]][["3","D"]]

わかりやすくするために、以下を含めました。

hand = ["3D", "4D", "4C", "5D", "JS", "JC"]

from itertools import groupby 

def generate_plays(hand):
    plays = []
    for rank,suit in groupby(hand, lambda f: f[0]):
        plays.append(list(suit))
    for card in hand:
        if card not in plays:       #redundant due to list nesting
            plays.append(list(card))       #problematic code in question
    return plays

出力:

[['3D'], ['4D', '4C'], ['5D'], ['JS', 'JC'], ['3', 'D'], ['4', 'D'], ['4', 'C'], ['5', 'D'], ['J', 'S'], ['J', 'C']]

期待される出力:

[['3D'], ['4D', '4C'], ['5D'], ['JS', 'JC'], ['4D'], ['4C'], ['5D'], ['JS'], ['JC']]

繰り返しますが、ここでの目的は、カード要素の連結性を for ループで維持することです。

どうもありがとう。

PS興味のある方は、1枚のカードと2つ以上の数字をプレイできるカードゲームのプレイジェネレーターです

4

1 に答える 1

2
hand = ["3D", "4D", "4C", "5D", "JS", "JC"]

from itertools import groupby 

def generate_plays(hand):
    plays = []
    for rank,suit in groupby(hand, lambda f: f[0]):
        plays.append(list(suit))
    for card in hand:
        if [card] not in plays:       #redundant due to list nesting
            plays.append([card])       #problematic code in question
    return plays

print generate_plays(hand)
于 2012-05-18T11:35:00.600 に答える