按字母表对表示卡片的元组列表进行排序,然后进行排名
Sorting list of tuples representing cards by alphabet and then rank
我想先按字母顺序(梅花、方块、红桃、黑桃)然后按等级(A 到 K)对一手牌进行排序。我有以下(非常冗长且效率低下)代码,但输出似乎有误。我觉得这是我使用的冒泡排序的问题。我在这里错过了什么,这样做的更好方法是什么?对于我的最终输出,我还将把 2、11、12 和 13 转换回 Ace、Jack、Queen 和 King,但我真的不想重做我首先将它们转换为数值的操作。 I/O 应如下所示:
示例输入:[('h', 'q'), ('c', 'j'), ('d', '2'), ( 's', '3'), ('h', '5'), ('d', 'j'), ('d', '10')]
示例输出:[('c', 'j'), ('d', '2'), ('d', '10'), ('d', 'j'), ('h', '5'), ('h', 'q'), ('s', '3')]
这是我目前的代码和结果输出:
def sort_hand(hand):
"""
Sorts the a hand of cards by the value of each suit/type of card.
"""
# A=1,2,3,4,5,6,7,8,9,10,J=11,Q=12,K=13
# UPDATING CARDS SO THAT THEY CAN BE ORDERED BY RANK
new_hand = []
# GIVING NUMERIC VALUES TO JACK, QUEEN, KING, AND ACE
for element in hand:
if element[1] == 'j':
val = element[1].replace('j', '11')
new_element = (element[0], val)
new_hand.append(new_element)
elif element[1] == 'q':
val = element[1].replace('q', '12')
new_element = (element[0], val)
new_hand.append(new_element)
elif element[1] == 'k':
val = element[1].replace('k', '13')
new_element = (element[0], val)
new_hand.append(new_element)
elif element[1] == 'a':
val = element[1].replace('a', '1')
new_element = (element[0], val)
new_hand.append(new_element)
else:
pass
new_hand.append(element)
# BUBBLE SORT USED TO ORDER BY RANK
for ix in range(1, len(new_hand)):
value_to_sort = new_hand[ix][1]
while new_hand[ix-1][1] > value_to_sort and ix > 0:
new_hand[ix], new_hand[ix-1] = new_hand[ix-1], new_hand[ix]
ix -= 1
# MAKE SUBLISTS FOR EACH SUIT
c_list = []
d_list = []
h_list = []
s_list = []
for element in new_hand:
if element[0] == 'c':
c_list.append(element)
elif element[0] == 'd':
d_list.append(element)
elif element[0] == 'h':
h_list.append(element)
else:
s_list.append(element)
# COMBINE ORDERED SUIT SUBLISTS TO MAKE FINAL SORTED HAND BY RANK
final_hand = c_list + d_list + h_list + s_list
return final_hand
>>> hand = [('h', 'q'), ('c', 'j'), ('d', '2'), ('s', '3'), ('h', '5'), ('d', 'j'), ('d', '10')]
>>> sort_hand(hand)
[('c', '11'),
('d', '10'),
('d', '11'),
('d', '2'),
('h', '12'),
('h', '5'),
('s', '3')]
我觉得我已经接近这段代码了,但不确定从这里到哪里去。希望得到解释清楚的答案,谢谢。
您可以使用内置的 sort
或 sorted
函数来实现此目的
上述功能还需要比较器逻辑,它可以使用 key
参数提供。
def sort_hand(inp):
##### I have created the order of the suites arbitrarily
suite_map = {
'h':1
,'c':2
,'s':3
,'d':4
}
rank_map = {
'a':1
'k':2
'q':3
'j':4
'10':5
'9':6
'8':7
'7':8
'6':9
'5':10
'4':11
'3':12
'2':13
'1':14
}
return suite_map[inp[0]],rank_map[inp[1]]
O/P --->
>>> hand
[('h', 'q'), ('c', 'j'), ('d', '2'), ('s', '3'), ('h', '5'), ('d', 'j'), ('d', '10')]
>>> sorted(hand,key=sort_hand)
[('h', 'q'), ('h', '5'), ('c', 'j'), ('s', '3'), ('d', 'j'), ('d', '10'), ('d', '2')]
您可以根据您的顺序控制基于 suite_map
和 rank_map
的字典顺序并相应地排序
使用 lambda 进行双键排序。
rank = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'j', 'q', 'k', 'a']
suit = ['c', 'd', 'h', 's']
inp = [('h', 'q'), ('c', 'j'), ('d', '2'), ('s', '3'), ('h', '5'), ('d', 'j'), ('d', '10')]
outp = sorted(inp,key = lambda x:
(suit.index(x[0]), rank.index(x[1])))
print(outp)
输出:
[('c', 'j'), ('d', '2'), ('d', '10'), ('d', 'j'), ('h', '5'), ('h', 'q'), ('s', '3')]
我想先按字母顺序(梅花、方块、红桃、黑桃)然后按等级(A 到 K)对一手牌进行排序。我有以下(非常冗长且效率低下)代码,但输出似乎有误。我觉得这是我使用的冒泡排序的问题。我在这里错过了什么,这样做的更好方法是什么?对于我的最终输出,我还将把 2、11、12 和 13 转换回 Ace、Jack、Queen 和 King,但我真的不想重做我首先将它们转换为数值的操作。 I/O 应如下所示:
示例输入:[('h', 'q'), ('c', 'j'), ('d', '2'), ( 's', '3'), ('h', '5'), ('d', 'j'), ('d', '10')]
示例输出:[('c', 'j'), ('d', '2'), ('d', '10'), ('d', 'j'), ('h', '5'), ('h', 'q'), ('s', '3')]
这是我目前的代码和结果输出:
def sort_hand(hand):
"""
Sorts the a hand of cards by the value of each suit/type of card.
"""
# A=1,2,3,4,5,6,7,8,9,10,J=11,Q=12,K=13
# UPDATING CARDS SO THAT THEY CAN BE ORDERED BY RANK
new_hand = []
# GIVING NUMERIC VALUES TO JACK, QUEEN, KING, AND ACE
for element in hand:
if element[1] == 'j':
val = element[1].replace('j', '11')
new_element = (element[0], val)
new_hand.append(new_element)
elif element[1] == 'q':
val = element[1].replace('q', '12')
new_element = (element[0], val)
new_hand.append(new_element)
elif element[1] == 'k':
val = element[1].replace('k', '13')
new_element = (element[0], val)
new_hand.append(new_element)
elif element[1] == 'a':
val = element[1].replace('a', '1')
new_element = (element[0], val)
new_hand.append(new_element)
else:
pass
new_hand.append(element)
# BUBBLE SORT USED TO ORDER BY RANK
for ix in range(1, len(new_hand)):
value_to_sort = new_hand[ix][1]
while new_hand[ix-1][1] > value_to_sort and ix > 0:
new_hand[ix], new_hand[ix-1] = new_hand[ix-1], new_hand[ix]
ix -= 1
# MAKE SUBLISTS FOR EACH SUIT
c_list = []
d_list = []
h_list = []
s_list = []
for element in new_hand:
if element[0] == 'c':
c_list.append(element)
elif element[0] == 'd':
d_list.append(element)
elif element[0] == 'h':
h_list.append(element)
else:
s_list.append(element)
# COMBINE ORDERED SUIT SUBLISTS TO MAKE FINAL SORTED HAND BY RANK
final_hand = c_list + d_list + h_list + s_list
return final_hand
>>> hand = [('h', 'q'), ('c', 'j'), ('d', '2'), ('s', '3'), ('h', '5'), ('d', 'j'), ('d', '10')]
>>> sort_hand(hand)
[('c', '11'),
('d', '10'),
('d', '11'),
('d', '2'),
('h', '12'),
('h', '5'),
('s', '3')]
我觉得我已经接近这段代码了,但不确定从这里到哪里去。希望得到解释清楚的答案,谢谢。
您可以使用内置的 sort
或 sorted
函数来实现此目的
上述功能还需要比较器逻辑,它可以使用 key
参数提供。
def sort_hand(inp):
##### I have created the order of the suites arbitrarily
suite_map = {
'h':1
,'c':2
,'s':3
,'d':4
}
rank_map = {
'a':1
'k':2
'q':3
'j':4
'10':5
'9':6
'8':7
'7':8
'6':9
'5':10
'4':11
'3':12
'2':13
'1':14
}
return suite_map[inp[0]],rank_map[inp[1]]
O/P --->
>>> hand
[('h', 'q'), ('c', 'j'), ('d', '2'), ('s', '3'), ('h', '5'), ('d', 'j'), ('d', '10')]
>>> sorted(hand,key=sort_hand)
[('h', 'q'), ('h', '5'), ('c', 'j'), ('s', '3'), ('d', 'j'), ('d', '10'), ('d', '2')]
您可以根据您的顺序控制基于 suite_map
和 rank_map
的字典顺序并相应地排序
使用 lambda 进行双键排序。
rank = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'j', 'q', 'k', 'a']
suit = ['c', 'd', 'h', 's']
inp = [('h', 'q'), ('c', 'j'), ('d', '2'), ('s', '3'), ('h', '5'), ('d', 'j'), ('d', '10')]
outp = sorted(inp,key = lambda x:
(suit.index(x[0]), rank.index(x[1])))
print(outp)
输出:
[('c', 'j'), ('d', '2'), ('d', '10'), ('d', 'j'), ('h', '5'), ('h', 'q'), ('s', '3')]