0
,我想知道如果有,讓我採取列表元素(["3D"]
)和一個特定的方法,使用一個for循環中,另一個列表中嵌套它([["3D"]]
),而避免了我在[["3","D"]]
中產生的當前類型轉換問題。保留在Python STR級聯元件列出轉換
爲了清楚起見,我添加了以下內容:
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循環。
非常感謝。
P.S.對於那些有興趣的玩家來說,它是一款紙牌遊戲的遊戲發生器,其中可以播放單張卡片和2+號碼。
您是否可以將卡片存儲爲元組以避免這種情況? –
創建'plays'列表可以簡化。 '從操作員導入itemgetter'' plays = [列表(訴訟)的等級,適合在groupby(手,itemgetter(0))]' – jamylak