1
I have a list of the cards and the suit of a deck that I need to sort. Considering the correct order: A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K And the suits: P(clubs). O(gold), E(sword), C(hearts) After reading the file with all the cards of all suits, I need to sort each suit. For example: reading the file returns (I will consider only a suit):
K P
A P
3 P
9 P
10 P
J P
4 P
2 P
6 P
8 P
7 P
Q P
5 P
I need to sort by the value of the cards. I thought I’d play in a dictionary where the keys are the cards and the value is the suit, and then sort by the key, but I can’t get out of it. Can someone help me? My code so far:
arquivo = open('nome_arquivo.txt', 'r')
dados = arquivo.readlines()
dic = {}
for linha in dados:
linha = linha.split()
dic[linha[0]] = linha[1]
print(dic)
Related: Data structure representing a deck of cards
– Woss
If you play the cards as keys and the values being the suits, you will get (if you are working with all the cards), four cases for each key. I believe this would pose a problem for the rest of the code. nested lists the first position being the value of the card and the second value the suit.
– Leonardo Oliveira