1

I currently have a program with a fairly large dictionary.

cards = {1.1:"Ace of Spades",
     1.2:"Ace of Clubs", 
     1.3:"Ace of Diamonds", 
     1.4:"Ace of Hearts", 

     2.1:"Two of Spades", 
     2.2:"Two of Clubs", 
     2.3:"Two of Diamonds",
     2.4:"Two of Hearts",

     3.1:"Three of Spades",
     3.2:"Three of Clubs",
     3.3:"Three of Diamonds",
     3.4:"Three of Hearts",

     4.1:"Four of Spades",
     4.2:"Four of Clubs",
     4.3:"Four of Diamonds",
     4.4:"Four of Hearts",

     5.1:"Five of Spades",
     5.2:"Five of Clubs",
     5.3:"Five of Diamonds",
     5.4:"Five of Hearts",

     6.1:"Six of Spades",
     6.2:"Six of Clubs",
     6.3:"Six of Diamonds",
     6.4:"Six of Hearts",

     7.1:"Seven of Spades",
     7.2:"Seven of Clubs",
     7.3:"Seven of Diamonds",
     7.4:"Seven of Hearts",

     8.1:"Eight of Spades",
     8.2:"Eight of Clubs",
     8.3:"Eight of Diamonds",
     8.4:"Eight of Hearts",

     9.1:"Nine of Spades",
     9.2:"Nine of Clubs",
     9.3:"Nine of Diamonds",
     9.4:"Nine of Hearts",

     10.1:"Ten of Spades",
     10.2:"Ten of Clubs",
     10.3:"Ten of Diamonds",
     10.4:"Ten of Hearts",

     11.1:"Jack of Spades",
     11.2:"Jack of Clubs",
     11.3:"Jack of Diamonds",
     11.4:"Jack of Hearts",

     12.1:"Queen of Spades",
     12.2:"Queen of Clubs",
     12.3:"Queen of Diamonds",
     12.4:"Queen of Diamonds",

     13.1:"King of Spades",
     13.2:"King of Clubs",
     13.3:"King of Diamonds",
     13.4:"King of Hearts"}

What I want to be able to do. Is for example print out the corresponding value for 11.2 (Jack of Clubs). And then I'd also want to print out the corresponding value for Jack of Clubs (11.2).

How would I do this?

EDIT

Also I want to add that of course it won't be the same value every time. It will be changed. The program generates a random number between 1 and 13 and then another between 1 and 4 then appends the two so it would be #.# then I'd want to retrieve the corresponding value for that float.

1 Answers1

1

Printing out the corresponding value for your randomly selected number will be easy:

print cards[randomNumber]

The problem occurs when you want to do the reverse lookup, and print out the value corresponding to "Jack of Clubs", or whatever the corresponding card name is. With a traditional dictionary, this will take a very long time, because it will involve searching through every value in the dictionary.

To avoid this, you could just create two dictionaries. However, it would be more efficient to use one of the suggestions in the Two way/reverse map question that David Zwicker linked to.

Community
  • 1
  • 1
seaotternerd
  • 6,298
  • 2
  • 47
  • 58