在我的 Rummy 程序中,我必须对输出进行排序,以便卡片以从高到低的值打印(K 是最高的,A 是最低的),例如:
输入:1. 8H、8C、8S、2C、7S、9H、KD
输出:1. 8S、8H、8C、KD、9C、7H、2C
run4 = []
set4 = []
run3 = []
set3 = []
other = []
cards = input('1. ')
cards = cards.split(', ')
card_numbers_count = {'A':0, '2':0, '3':0, '4':0, '5':0, '6':0, '7':0, '8':0, '9':0, 'T':0, 'J':0, 'Q':0, 'K':0}
card_suit_count = {'H':0, 'D':0, 'S':0, 'C':0}
def card_suit_key(card):
suit_vals = {'S': 1, 'H': 2, 'C': 3, 'D': 4}
return suit_vals[card[1]]
def numerical_order(card):
num_vals = {'K': 1, 'Q': 2, 'J': 3, 'T': 4, '9': 5, '8': 6, '7': 7, '6': 8, '5': 9, '4': 10, '3': 11, '2': 12, 'A': 13}
return num_vals[card[1]]
# card value
for card in cards:
card_number = card[:-1]
card_numbers_count[card_number] += 1
for card in cards:
card_number = card[:-1]
if card_numbers_count[card_number] == 3:
run3.append(card)
run3.sort(key=card_suit_key)
elif card_numbers_count[card_number] == 4:
run4.append(card)
run4.sort(key=card_suit_key)
elif card_numbers_count[card_number] > 3 or card_numbers_count[card_number] < 3:
other.append(card)
print (other)
#other.sort(key=numerical_order)
print (run4, run3, other)
# card suit
for card in cards:
card_suit = card[1:]
card_suit_count[card_suit] += 1
for card in cards:
card_suit = card[1:]
我已经编辑了这个问题,因为我能够做到从最高到最低订购西装,但我不能对卡值做同样的事情。任何帮助,将不胜感激。谢谢。