I'm having trouble with an exercise from a book, here it is;

" Write a program that reads the values for a random list of card from a file,(cards.txt) where each line in the file represents a single card with the rank and then suit seperated by a space. The rank should be an integer in the range 1 - 13 (Ace:1, King:13), while the suit should be lower case character (h, d, c, s). Sort the cards into suits ordered by rank, and print out the ordered list. Hint: sort the list first by rank, and then suit."

If anyone could give me a few pointers on how to do this exercise it would be greatly appreciated

If I understand this right you want something similar to this ...

# take a random hand of cards where each card is identified by
# string 'rank suit' and sort by rank and suit
def cards_str2tup(cards):
    convert each card 'rank suit' string to a (suit, rank) tuple
    where rank is an integer number
    cards_tup = []
    for card in cards:
        rank, suit = card.split()
        cards_tup.append((suit, int(rank)))
    return cards_tup
def cards_tup2str(cards_tup):
    convert each card (suit, rank) tuple to a 'rank suit' string
    cards = []
    space = ' '
    for tup in cards_tup:
        suit, rank = tup
        s = str(rank) + space + suit
    return cards
# assume random hand of five cards
hand = ['4 c', '2 s', '12 h', '8 s', '6 s']
# this will sort looking at rank number as a string
print hand  # oops  ['12 h', '2 s', '4 c', '6 s', '8 s']
# to sort properly convert to a list of (suit, int(rank)) tuples
# by default:
# suit, the first item in each tuple will be primary sort criteria
# rank, the second item in each tuple will be secondary sort criteria
cards_tup = cards_str2tup(hand)
# suits are in order and each suit's rank is in order too
print cards_tup  # [('c', 4), ('h', 12), ('s', 2), ('s', 6), ('s', 8)]
# convert the properly sorted list of tuples to a list of strings
cards = cards_tup2str(cards_tup)
print cards  # ['4 c', '12 h', '2 s', '6 s', '8 s']

Thanks for that mate, Now i understand where i was having trouble. i couldn't get my head around the classes. Just one more question, As the cards are sorted into a string, how do you go about placing them in a list .i.e. one card per line (placing newline char \n).

Here would be an example how to read the cards file into a list ...

the cards.txt file looks something like this -->
4 c
10 d
12 s
6 d
9 c
12 d
4 h
9 s
7 d
8 d
3 c
3 h
13 h
4 s
def read_cards(filename):
    read the shuffeled cards from a text file
    that has one card string per line
    cards = []
    for card in open(filename, 'r'):
        # strip the trailing newline character
    return cards
# read the deck of cards from a file
filename = 'cards.txt'
cards = read_cards(filename)
# test
print cards  # ['4 c', '10 d', '12 s', '6 d', '9 c', ...]

Yeah sorry, i understand how to read a file into the program but the out put is returned in a sinlge line string eg./ etc...
what i ment to ask was how do you get this returned string (above) to print in list form,
6 h
7 h
8 h
9 h
and so on...

i hope i'm making sence to you

I think you are using wrong word here, list is like an array. You simply want each element of list displayed on separate line, right?

# for instance this is list
cards = ['4 c', '12 h', '2 s', '6 s', '8 s']
# one list item per line display
for card in cards:
    print card
my output =
4 c
12 h
2 s
6 s
8 s

Edit: changed php tags, don't work properly

This question has already been answered. Start a new discussion instead.