Python3 - 将数组排序为特定顺序
Python3 - Sort array into specific order
我正在 Python 3 中编写纸牌游戏 (Kalashnikov),我想对玩家的手牌进行排序。是否可以使用一种字典来对手牌进行排序,以便重要的牌按正确的顺序排列?不知道用什么方法。
游戏的目的是在4张牌中得到A、K、4、7,所以我需要将手中的牌按顺序排列:
- 一个
- K
- 4
- 7
- [其他]
如果原来的手牌是3、K、7、2,比如排序后是这样的:
- K
- 7
- 3
- 2
或:
- K
- 7
- 2
- 3
我当前的代码(经过简化以删除不必要的内容)是:
deck = shuffle()
print("Dealing", end="", flush=True)
for i in range(4):
print(".", end="")
if player == 1:
hand.append(deck.pop())
oppHand.append(deck.pop())
else:
oppHand.append(deck.pop())
hand.append(deck.pop())
sleep(1.25)
hand = sortHand(hand)
oppHand = sortHand(oppHand)
print(" [DONE]")
函数 sortHand(hand)
应该是什么?
Is it possible to use a sort of dictionary to sort the hands so that the important cards are in the correct order? I have no idea what method would be used.
Python 的 sorted
built-in 函数(以及 list.sort
方法)有一个 key
参数,这是您在这里需要的参数: key
是一个将值转换为用于排序的“等级”的函数,例如如果您 return 0 表示“A”,1 表示“K”,那么“A”将排在“K”之前。
您可以只定义一个等级字典,然后将其用作键:
import collections
ranks = collections.defaultdict(lambda: 5, {
'A': 0,
'K': 1,
'4': 3,
'7': 4,
})
hand = list('3K72')
print('before', ', '.join(hand))
# => before 3, K, 7, 2
hand.sort(key=lambda card: ranks[card])
print(' after', ', '.join(hand))
# => after K, 7, 3, 2
您实际上可以利用带有可选键变量的标准库排序函数:
def priority(element):
if element == 'A':
return -4
if element == 'K':
return -3
if element == '4':
return -2
if element == '7':
return -1
return ord( element )
print(sorted(['1','4','K','7'], key=priority))
一个简短的:hand.sort(key='74KA'.find, reverse=True)
注意find
returns -1
如果找不到该值,则它将字符 A、K、4、7 映射到(索引)3、2、 1、0 和所有其他字符为 -1。这是所需顺序的相反顺序,因此 reverse=True
将其转换为所需顺序。
我正在 Python 3 中编写纸牌游戏 (Kalashnikov),我想对玩家的手牌进行排序。是否可以使用一种字典来对手牌进行排序,以便重要的牌按正确的顺序排列?不知道用什么方法。
游戏的目的是在4张牌中得到A、K、4、7,所以我需要将手中的牌按顺序排列:
- 一个
- K
- 4
- 7
- [其他]
如果原来的手牌是3、K、7、2,比如排序后是这样的:
- K
- 7
- 3
- 2 或:
- K
- 7
- 2
- 3
我当前的代码(经过简化以删除不必要的内容)是:
deck = shuffle()
print("Dealing", end="", flush=True)
for i in range(4):
print(".", end="")
if player == 1:
hand.append(deck.pop())
oppHand.append(deck.pop())
else:
oppHand.append(deck.pop())
hand.append(deck.pop())
sleep(1.25)
hand = sortHand(hand)
oppHand = sortHand(oppHand)
print(" [DONE]")
函数 sortHand(hand)
应该是什么?
Is it possible to use a sort of dictionary to sort the hands so that the important cards are in the correct order? I have no idea what method would be used.
Python 的 sorted
built-in 函数(以及 list.sort
方法)有一个 key
参数,这是您在这里需要的参数: key
是一个将值转换为用于排序的“等级”的函数,例如如果您 return 0 表示“A”,1 表示“K”,那么“A”将排在“K”之前。
您可以只定义一个等级字典,然后将其用作键:
import collections
ranks = collections.defaultdict(lambda: 5, {
'A': 0,
'K': 1,
'4': 3,
'7': 4,
})
hand = list('3K72')
print('before', ', '.join(hand))
# => before 3, K, 7, 2
hand.sort(key=lambda card: ranks[card])
print(' after', ', '.join(hand))
# => after K, 7, 3, 2
您实际上可以利用带有可选键变量的标准库排序函数:
def priority(element):
if element == 'A':
return -4
if element == 'K':
return -3
if element == '4':
return -2
if element == '7':
return -1
return ord( element )
print(sorted(['1','4','K','7'], key=priority))
一个简短的:hand.sort(key='74KA'.find, reverse=True)
注意find
returns -1
如果找不到该值,则它将字符 A、K、4、7 映射到(索引)3、2、 1、0 和所有其他字符为 -1。这是所需顺序的相反顺序,因此 reverse=True
将其转换为所需顺序。