0

I'm expecting this code to print spade:A spade:2 and so on until heart:K.
But it only does heart:A to heart:K.
How should I do it?

symbols = ["spade", "clover", "diamond", "heart"]
numbers = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",       "K"]
cards = {}

for num in numbers:
    for symbol in symbols:
        cards[num] = symbol

print cards
4
  • 1
    You are replacing the values for cards. You can only have one value for each numbers key, so the last cards[num] = symbol is going to win. Commented Jan 26, 2015 at 17:39
  • 1
    Also, your code would print {'A': 'heart', '10': 'heart', 'K': 'heart', 'J': 'heart', ...}, because 'heart' is the last entry in symbols. Commented Jan 26, 2015 at 17:40
  • @MartijnPieters I didn't understand ur first comment... can u pls elaborate? Commented Jan 26, 2015 at 17:40
  • 1
    Dictionaries map one key to one value. You are replacing that value each time. First you set cards['A'] to 'spade', then to 'clover', then 'diamond' then finally 'heart'. You cannot add multiple 'A' keys to a dictionary. Commented Jan 26, 2015 at 17:52

3 Answers 3

1

Use your itertools toolbox

import itertools

symbols = ["spade", "clover", "diamond", "heart"]
numbers = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",       "K"]

combinations = itertools.product(symbols, numbers)

cards = ["{}:{}".format(suit, rank) for suit,rank in combinations]

This will give you the list:

['spade:A',
 'spade:2',
 'spade:3',
 'spade:4',
 'spade:5',
 'spade:6',
 'spade:7',
 'spade:8',
 'spade:9',
 'spade:10',
 'spade:J',
 'spade:Q',
 'spade:K',
 'clover:A',
 'clover:2',
 'clover:3',
 'clover:4',
 'clover:5',
 'clover:6',
 'clover:7',
 'clover:8',
 'clover:9',
 'clover:10',
 'clover:J',
 'clover:Q',
 'clover:K',
 'diamond:A',
 'diamond:2',
 'diamond:3',
 'diamond:4',
 'diamond:5',
 'diamond:6',
 'diamond:7',
 'diamond:8',
 'diamond:9',
 'diamond:10',
 'diamond:J',
 'diamond:Q',
 'diamond:K',
 'heart:A',
 'heart:2',
 'heart:3',
 'heart:4',
 'heart:5',
 'heart:6',
 'heart:7',
 'heart:8',
 'heart:9',
 'heart:10',
 'heart:J',
 'heart:Q',
 'heart:K']
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is that you are not iterating the right way and thus you are not appending in the list. The right way to do it is

symbols = ["spade", "clover", "diamond", "heart"]
numbers = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",         "K"]
cards = []

for j in range(len(symbols)):
    for i in range(len(numbers)):
       cards.append(str(symbols[j]+':'+str(numbers[i])))

print cards

with output:

['spade:A', 'spade:2', 'spade:3', 'spade:4', 'spade:5', 'spade:6', 'spade:7',  'spade:8', 
'spade:9', 'spade:10', 'spade:J', 'spade:Q', 'spade:K', 'clover:A', 'clover:2',
'clover:3', 'clover:4', 'clover:5', 'clover:6', 'clover:7', 'clover:8', 'clover:9',
'clover:10', 'clover:J', 'clover:Q', 'clover:K', 'diamond:A', 'diamond:2', 'diamond:3',
'diamond:4', 'diamond:5', 'diamond:6', 'diamond:7', 'diamond:8', 'diamond:9', 'diamond:10',
'diamond:J', 'diamond:Q', 'diamond:K', 'heart:A', 'heart:2', 'heart:3', 'heart:4',
'heart:5', 'heart:6', 'heart:7', 'heart:8', 'heart:9', 'heart:10', 'heart:J', 'heart:Q', 'heart:K']

Made with Ipython Notebook in python 2.7

Hope it helps.

6 Comments

Also, in Python - you generally don't iterate over the indices of sequences, just the sequencing themselves. In this case, a list-comp can be used to simplify the code and can be reduced to: cards = ['{}:{}'.format(s, n) for s in symbols for n in numbers]
Seems to be an exotic way to show you are good in Python (which I am sure you do), but cannot be easily understood (or thought) by everyone. Anyway thanks for the information.
I agree in some sense - I'm pointing it out because it's a very common thing in the world of Python - who knows - SO is meant to be a resource of information, this post will be read by future visitors - if something comes in handy - all the better!
@PanagiotisBarkoutsos: Is this one more readable, in your opinion? cards = [s + ':' + n for s in symbols for n in numbers]
I can easily get what you mean, but I am working on Python a lot. For someone asking such a question, means he is in beginner level and do not want to overcomplicate the structures. Everything is readable and can be understood, but the thing is that you need to explain why his idea is not working and not to provide a way that he will not be able to implement in future use. I wrote what I wrote based on the format the guy asking used. I could write what you propose but it would not be a correction, it would be just a different way to express it.
|
0

You are iterating the symbols just fine but when you are going over the numbers in the second loop, you are actually replacing the values set by the previous loop hence you only have values from the last loop left and everything is replaced. This means cards["A"] value is set 4 times in the loop and the last for the "heart" is retained. The same thing is happening for all the other indexes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.