I have to generate 13 cards in random the output must in ascending order or from highest in ranking, it is as follows
Ranking of suits from lowest to highest is Clubs, Diamonds, Hearts, Spades Ranking of values from lowest to highest is 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A
My code prints 13 random cards but there is no order. How can I sort it according to the ranking above?
This is my code:
import itertools
import random
value = ['A','2','3','4','5','6','7','8','9','10','J','Q','K']
suits = ['C','D','H','S']
deck = list(itertools.product(vals, suits))
random.shuffle(deck)
for val, suit in deck:
print('%s-%s' % (val, suit))
The output should look like this:
A-C,2-C,3-C,4-C,5-C,6-C,7-C,8-C,9-C,10-C,J-C,Q-C,K-C
CodePudding user response:
Printing
You can take advantage of the sep
argument to print
. We'll flatten a generator expression to create string representations of each card, and specify these separate items printed should be separated by ','
with '\n'
being printed at the end.
print(*(f"{c}-{s}" for c, s in deck), sep=',', end='\n')
Output:
2-S,A-S,3-C,9-D,A-D,9-C,5-C,3-H,A-C,5-H,3-D,4-S,K-D,2-H,Q-C,5-S,J-S,6-D,2-C,4-C,4-H,8-S,3-S,J-D,A-H,J-C,6-C,8-D,9-H,4-D,10-H,9-S,6-S,7-H,7-D,K-C,10-D,8-H,2-D,8-C,Q-S,K-H,7-C,5-D,J-H,10-S,K-S,7-S,Q-D,6-H,Q-H,10-C
Ordering
For order, I'm not sure why you are shuffling if you want the cards in order.
Given the order you want, you should reorder your lists slightly, and then use a list comprehension to generate the card combos.
value = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
suits = ['C', 'D', 'H', 'S']
deck = [(c, s) for c in value for s in suits]
# [('2', 'C'), ('2', 'D'), ('2', 'H'), ('2', 'S'), ('3', 'C'), ('3', 'D'), ('3', 'H'), ('3', 'S'), ('4', 'C'), ('4', 'D'), ('4', 'H'), ('4', 'S'), ('5', 'C'), ('5', 'D'), ('5', 'H'), ('5', 'S'), ('6', 'C'), ('6', 'D'), ('6', 'H'), ('6', 'S'), ('7', 'C'), ('7', 'D'), ('7', 'H'), ('7', 'S'), ('8', 'C'), ('8', 'D'), ('8', 'H'), ('8', 'S'), ('9', 'C'), ('9', 'D'), ('9', 'H'), ('9', 'S'), ('10', 'C'), ('10', 'D'), ('10', 'H'), ('10', 'S'), ('J', 'C'), ('J', 'D'), ('J', 'H'), ('J', 'S'), ('Q', 'C'), ('Q', 'D'), ('Q', 'H'), ('Q', 'S'), ('K', 'C'), ('K', 'D'), ('K', 'H'), ('K', 'S'), ('A', 'C'), ('A', 'D'), ('A', 'H'), ('A', 'S')]
Now the same print as before yields:
2-C,2-D,2-H,2-S,3-C,3-D,3-H,3-S,4-C,4-D,4-H,4-S,5-C,5-D,5-H,5-S,6-C,6-D,6-H,6-S,7-C,7-D,7-H,7-S,8-C,8-D,8-H,8-S,9-C,9-D,9-H,9-S,10-C,10-D,10-H,10-S,J-C,J-D,J-H,J-S,Q-C,Q-D,Q-H,Q-S,K-C,K-D,K-H,K-S,A-C,A-D,A-H,A-S
Sorting
If you want to shuffle the deck, take 13 cards at random, then sort those, you can use sorted
with a lambda function that generates a tuple of the position of the value and the suit in the original lists.
random.shuffle(deck)
top13 = deck[:13]
top13_sorted = sorted(top13, key=lambda card: (value.index(card[0]), suits.index(card[1])))
For a run here, top13_sorted
is now:
[('2', 'S'), ('3', 'D'), ('3', 'S'), ('4', 'C'), ('4', 'S'), ('7', 'D'), ('7', 'H'), ('8', 'H'), ('9', 'C'), ('10', 'C'), ('J', 'C'), ('J', 'H'), ('K', 'H')]
CodePudding user response:
You didn't mention which list has sorting priority, and your example doesn't really help since all the cards have the same suit.
If you want them sorted by value first and then by suit you can use:
sorting by value then by suit
random.shuffle(deck)
sorted_deck = sorted(deck, key=lambda x: (value.index(x[0]) * (len(value) 1)) suits.index(x[1]))
or if you want them sorted by suit first you could use
Sorting by suit then by value
random.shuffle(deck)
sorted_deck = sorted(deck, key=lambda x: value.index(x[0]) (suits.index(x[1]) * (len(suits) 1)))
If you only want 13 cards then all you need to do is slice your unsorted list before feeding it to the sorting algorithm:
random.shuffle(deck)
deck = deck[:13]
# then use the sorting algorithms above
Then to print them all on one line you could do:
print(','.join(['-'.join(i) for i in sorted_deck]))
output:
# sorting by suit then by value
A-C,2-C,3-C,4-C,5-C,6-C,7-C,8-C,9-C,10-C,J-C,Q-C,K-C,A-D,2-D,3-D,4-D,5-D,6-D,7-D,8-D,9-D,10-D,J-D,Q-D,K-D,A-H,2-H,3-H,4-H,5-H,6-H,7-H,8-H,9-H,10-H,J-H,Q-H,K-H,A-S,2-S,3-S,4-S,5-S,6-S,7-S,8-S,9-S,1
0-S,J-S,Q-S,K-S
# sorting by value then by suit
A-C,A-D,A-H,A-S,2-C,2-D,2-H,2-S,3-C,3-D,3-H,3-S,4-C,4-D,4-H,4-S,5-C,5-D,5-H,5-S,6-C,6-D,6-H,6-S,7-C,7-D,7-H,7-S,8-C,8-D,8-H,8-S,9-C,9-D,9-H,9-S,10-C,10-D,10-H,10-S,J-C,J-D,J-H,J-S,Q-C,Q-D,Q-H,Q-S,
K-C,K-D,K-H,K-S
CodePudding user response:
Reference to Sort a list based on a given order
You should first create the list of order of how the card deck should be, which is as 2-C, 2-D, .... A-S
To do so,
value = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
suits = ['C','D','H','S']
#generate the key
srt = {f"{a}-{b}": x*4 y for x,a in enumerate(value) for y,b in enumerate(suits)}
Based on the key above, you can now sort your deck. Your deck should be
deck = list(itertools.product(value, suits))
random.shuffle(deck)
#change the deck from list to string
deck = [f'{y[0]}-{y[1]}' for y in deck[:13]]
sorted(deck, key=lambda x: srt[x])
the result should be now in ascending order as you wish. (Refer to below for result)
['2-C', '2-H', '3-H', '4-D', '5-C', '5-H', '6-C', '6-D', '6-H', '7-C', '8-D', '9-C', 'K-S']
** Edited, add on Result ** Edited, add on to get 13 card only
CodePudding user response:
This method uses a helper function as key for the card sorting. A deck is constructed, shuffled and 13 cards are taken. Then the hand is sorted. Ace is low in this case.
import random, itertools
values = ['A', '2','3','4','5','6','7','8','9','10','J','Q','K']
suits = ['C','D','H','S']
def card_value(card):
val, suit = card.split('-')
return suits.index(suit) 10*values.index(val)
deck = [f'{v}-{s}' for v,s in itertools.product(values, suits)]
random.shuffle(deck)
hand = deck[:13]
print(sorted(hand, key=card_value, reverse=True))
CodePudding user response:
I think your problem can be divided into two tasks:
- Get 13 cards randomly from the deck.
- Sort these random cards.
Code or solution for both are given in above answers. You just need to merge them. Part 1 is almost already done by your code. Just tweak a little bit of your code and you'll complete the first task. Then use the list from Task#1 and sort it based on the ranks.
Hope it'll help :)