I am developing a text based game of Go Fish as a way of practicing/learning python. I want to sort the cards in the players hand for readability and am running into issues.
我正在开发一种基于文本的Go Fish游戏,作为练习/学习python的一种方式。我想把玩家手中的牌进行分类,以提高可读性,同时也会遇到一些问题。
my_hand = [('3', 'Spades'), ('Ace', 'Hearts'), ('10', 'Clubs'), ('King', 'Diamonds'), ('4', 'Clubs')]
if I do:
如果我做的事:
my_hand.sort()
I get:
我得到:
[('10', 'Clubs'), ('3', 'Spades'), ('4', 'Clubs'), ('Ace', 'Hearts'), ('King', 'Diamonds')]
I don't want the 10 to come first. But because I have 'Ace', 'King' 'Queen' 'Jack' as card values I can't sort as an int.
我不想让这10个人先来。但因为我有“Ace”,“King”“Queen”“Jack”作为card值,我不能将其排序为int。
this is what I came up with. But I'm thinking there has to be a better way to do it, and the goal of this project is to learn new methods so I thouhgt I'd ask.
这就是我想到的。但是我认为必须有更好的方法来完成它,这个项目的目标是学习新的方法,所以我想我会问。
my solution:
我的解决方案:
def natural_sort(hand):
number_cards = []
ten_cards = []
face_cards = []
new_hand = []
for card in hand:
try:
int(card[0])
if card[0] != 10:
number_cards.append(card)
elif card[0] == 10:
ten_cards.append(card)
except ValueError:
face_cards.append(card)
number_cards.sort()
face_cards.sort()
new_hand.extend(number_cards)
new_hand.extend(ten_cards)
new_hand.extend(face_cards)
return new_hand
Thoughts?
想法吗?
2 个解决方案
#1
0
What you can do is assign a number value to your cards, as there are only 4 face cards. For example, say Ace=1, Jack=11, and so on.
你能做的是给你的卡片分配一个数字值,因为只有4张脸卡。例如,Ace=1, Jack=11,等等。
#2
0
This is a nice use-case for a custom comparator! A comparator is a function used by a sorting algorithm to compare two items. cmp(a, b) returns -1 if a < b, 0 if a == b, and 1 if a > b.
对于自定义比较器来说,这是一个很好的用例!比较器是排序算法用来比较两个项目的函数。cmp(a, b)返回-1,如果a < b,则为0,如果a == b,如果> b为1。
To get the sort you want, you could do:
为了得到你想要的那种,你可以:
hand = [('10', 'Clubs'), ('3', 'Spades'), ('4', 'Clubs'), ('Ace', 'Hearts'), ('King', 'Diamonds')]
def card_cmp(card1, card2):
try:
c1num = int(card1[0])
except ValueError:
c1num = False
try:
c2num = int(card2[0])
except ValueError:
c2num = False
if c1num and c2num:
return cmp(c1num, c2num)
elif c1num:
return -1
elif c2num:
return 1
else:
return cmp(card1[0], card2[0])
print sorted(hand, cmp=card_cmp)
If you also want to sort the face cards by value rather than alphabetically, you'd have to encode that order somewhere, maybe with a dictionary like
如果您还想按值而不是按字母顺序对面孔卡片进行排序,那么您需要在某个地方编码该顺序,可能需要使用字典。
face_cards = {
'Jack' : 11,
'Queen' : 12,
'King' : 13,
'Ace' : 14,
}
and then your comparator could just be
然后你的比较器就可以了。
def card_cmp(card1, card2):
try:
c1num = int(card1[0])
except ValueError:
c1num = face_cards[card1[0]]
try:
c2num = int(card2[0])
except ValueError:
c2num = face_cards[card2[0]]
return cmp(c1num, c2num)
#1
0
What you can do is assign a number value to your cards, as there are only 4 face cards. For example, say Ace=1, Jack=11, and so on.
你能做的是给你的卡片分配一个数字值,因为只有4张脸卡。例如,Ace=1, Jack=11,等等。
#2
0
This is a nice use-case for a custom comparator! A comparator is a function used by a sorting algorithm to compare two items. cmp(a, b) returns -1 if a < b, 0 if a == b, and 1 if a > b.
对于自定义比较器来说,这是一个很好的用例!比较器是排序算法用来比较两个项目的函数。cmp(a, b)返回-1,如果a < b,则为0,如果a == b,如果> b为1。
To get the sort you want, you could do:
为了得到你想要的那种,你可以:
hand = [('10', 'Clubs'), ('3', 'Spades'), ('4', 'Clubs'), ('Ace', 'Hearts'), ('King', 'Diamonds')]
def card_cmp(card1, card2):
try:
c1num = int(card1[0])
except ValueError:
c1num = False
try:
c2num = int(card2[0])
except ValueError:
c2num = False
if c1num and c2num:
return cmp(c1num, c2num)
elif c1num:
return -1
elif c2num:
return 1
else:
return cmp(card1[0], card2[0])
print sorted(hand, cmp=card_cmp)
If you also want to sort the face cards by value rather than alphabetically, you'd have to encode that order somewhere, maybe with a dictionary like
如果您还想按值而不是按字母顺序对面孔卡片进行排序,那么您需要在某个地方编码该顺序,可能需要使用字典。
face_cards = {
'Jack' : 11,
'Queen' : 12,
'King' : 13,
'Ace' : 14,
}
and then your comparator could just be
然后你的比较器就可以了。
def card_cmp(card1, card2):
try:
c1num = int(card1[0])
except ValueError:
c1num = face_cards[card1[0]]
try:
c2num = int(card2[0])
except ValueError:
c2num = face_cards[card2[0]]
return cmp(c1num, c2num)