詞典列表我有一個列表:從列表中進行的關鍵
['8C', '2C', 'QC', '5C', '7C', '3C', '6D', 'TD', 'TH', 'AS',
'QS', 'TS', 'JS', 'KS']
我需要一個字典是這樣的:(排序並不重要)
{'C': ['QC', '8C', '7C', '5C', '3C', '2C'],
'S': ['AS', 'KS', 'QS', 'JS', 'TS']
}
代碼:
def parse_flush(cards):
cards = sort_by_color(cards)
flush_dic = {}
print str(cards)
count = 0
pos = 0
last_index = 0
for color in colors:
for i, card in enumerate(cards):
if card[1] == color:
count += 1
last_index = i+1
if count == 1:
pos = i
if count >= 5:
flush_dic[color] = sort_high_to_low(cards[pos:last_index])
count = 0
return flush_dic
我的代碼現在看起來,它的工作原理,但我不喜歡它的長度,就可以使用Python的技巧把它縮短?
你能解釋一下'GROUPBY(排序(數據,鍵= keyFn),keyFn'我 –
謝謝。你的,這是作品 – Lucian
@GrijeshChauhan請檢查我在答案中添加的解釋,並請讓我知道它是否合理。 – thefourtheye