Why do you want to keep two lists, a card has both a rank and a suit, they belong together to make the card. If you just shuffle the rank list, then you lost the connection with the suit list. As I mentioned before, keep your cards in a list of (rank, suit) tuples. That list will by default sort by rank (item 0 of the tuple) and keep the suit along. The same goes for the random.shuffle().
The following code is suppose to print a pack of cards in order, then shuffle them and reprint them again. Using Deck to print them it's not quite how I would like it.
This is how its printing...
[['1', 'h'], ['2', 'h'], ['3', 'h']...
I would like it to print:
Ace of Hearts
2 of Hearts
...
my file is saved like...
1 of h
2 of h
...
[php]
import string
class Card:
def __init__(self,suit,rank):
self.suit = suit
self.rank = rank
suitList = []
rankList = []
def __repr__(self):
return str(self)
def __str__(self):
return "%s of %s" % (self.rankList[self.rank],self.suitList[self.suit])
class Deck():
def __init__(self):
self.cards = []
for suit in range(4):
for rank in range(1,14):
self.cards.append(Card(suit,rank))
def printDeck(self):
for card in self.cards:
print card
def __str__(self):
s = ""
for i in range(len(self.cards)):
s = s + " " + str(self.cards[i]) + '\n'
return s
def shuffle(self):
import random
nCards = len(self.cards)
for i in range(nCards):
j = random.randrange(i, nCards)
[self.cards[i], self.cards[j]] = [self.cards[j], self.cards[i]]
def main():
a = open('cards.txt','r')
line = a.readlines()
a.close()
deck = Deck()
cards =[]
for i in line:
cards.append(i.split())
print deck
#replace suit with full word
for index, suit in enumerate(Card.suitList):
if suit == 'h':
Card.suitList[index] = "Hearts"
elif suit == 'c':
Card.suitList[index] = "Clubs"
elif suit == 's':
Card.suitList[index] = "Spades"
elif suit == 'd':
Card.suitList[index] = "Diamonds"
print[/php]