1

What I am trying to do is to store an x,y value with a key. For example, in my case, I need it so that once any x,y has reached the value of 3, some process happens.

Here is what I have tried: However, this gives me an error, as I am unable to store lists within dictionaries.

dictionary= {}
player = [0,0]

def Checking():
    if player in dictionary:
        dictionary[[player]] +=1
        print("repopulate", dictionary)
    else:
        dictionary[player] = 0
        print(dictionary)
    if dictionary.get(player) >= 3:
        print("It is done!")

EDIT: Sorry about the lack of clarity in the question. The player variable is the user input of where the user wishes to move within the x,y given. There are multiple treasures, and if the user is to chose a position x,y which is the same as a treasure x,y; then +1 should be added to that treasure. Once a treasure reaches 3, it should be diminished.

Test1234
  • 113
  • 3
  • 15

3 Answers3

0

If you save you list x,y in the dictionary like this, that also depends on your condition when you want to change the values you have to iterate through the list.

treasureHits = {}
player = [0,0]
def myFunction():
    treasureHits["key"]=[p+1 for p in player]
    print treasureHits["key"]
myFunction()

That's what I understand from your question.

bhansa
  • 7,282
  • 3
  • 30
  • 55
0

So the easiest way to do this is to call str() on the player variable. This will turn the list into a string. You need to do this because dictionaries cannot use lists as keys. So try the following: treasureHits[str(player)]

Note thought that this is usually not recommended because you will have very unusual keys and if anyone else will be editing your code it could be confusing. Make sure to leave plenty of comments in your code!

NendoTaka
  • 1,224
  • 8
  • 14
0

I think you want to use player as your key, and the count as the value of that key:

>>> treasure_hits = {}
>>> player = (0,0)
>>> try:
...   treasure_hits[player] += 1
... except KeyError:
...   treasure_hits[player] = 0
... 
>>> treasure_hits
{(0, 0): 0}
>>> 
>>> try:
...   treasure_hits[player] += 1
... except KeyError:
...   treasure_hits[player] = 0
... 
>>> treasure_hits
{(0, 0): 1}
>>> 

Making treasure_hits a tuple instead of a list allows it to be used as a key since it is immutable

joel goldstick
  • 4,393
  • 6
  • 30
  • 46
  • Sorry about the lack of clarity in the question. The player variable is the user input of where the user wishes to move within the x,y given. There are multiple treasures, and if the user is to chose a position x,y which is the same as a treasure x,y; then +1 should be added to that treasure. Once a treasure reaches 3, it should be diminished. – Test1234 Mar 26 '16 at 17:20
  • The only think substantially different about your code and mine is that you use a list as a key, and you can't do that. But if you do tuple(player) you can use it as a key – joel goldstick Mar 26 '16 at 17:22
  • While tuples *are* immutable, the important aspect is that they are **hashable**. [Related question](http://stackoverflow.com/questions/2671376/hashable-immutable) – Jared Goguen Mar 26 '16 at 17:36
  • Thanks alot for the quick help guys! much appreciated, and as a new member to stackoverflow, I am really fascinated to see how helpful the community is. Keep it up guys! – Test1234 Mar 26 '16 at 17:39