0

I have two dictionaries in a program I'm writing for fun to get practice dealing with .jsons in Python. I want my program to take any player's .json file (actually pretty easy to obtain) and output how many runes of each rune type the player owns. It's basically my first week of learning about data in Python (or any language) so I'm still very new to this.

The first dictionary, I created like this:

rune_set_counts = {}
for i in range(1, 24):
    rune_set_counts[i] = 0

I made the range begin with 1 because the game's .json indexes rune sets using values from 1 to 23.

The second dictionary contains 23 keys, and each key is the string that is the actual name of that particular rune type in the game. So, for example, in rune_set_counts, the first key is 1. In my second dictionary, rune_set_translated_counts, the first key is the corresponding name, "Energy".

I would like to make a function that transposes the values from the first dictionary to the second dictionary. If rune_set_counts[1] = 5, I want rune_set_translated_counts[Energy] = 5.

This was my attempt at a function:

def convert_to_rune_set(old_dict, new_dict):
    for i in range(len(old_dict)):
        frequency = old_dict[i+1]
        new_dict[i] = frequency

The problem is, I tried that and it just added all 23 key-value pairs from the old dictionary to the new one, lol. I don't want 46 keys. How should I do this?

KGE
  • 125
  • 10
  • Are trying to do something like `new_dict[old_dict[i+1]]` or swapping the values between two dictionaries? – Roy May 24 '21 at 06:22

2 Answers2

1

As @Tim Roberts mentioned in his answer, Dictionary does not have order, you will need a map of number vs their names in rune. Try something like this.

from random import randint

rune_set_counts = {}
for i in range(1, 24):
    rune_set_counts[i] = randint(0, 10)

print(rune_set_counts)
# prints {1: 0, 2: 0, 3: 6, 4: 5, 5: 0, 6: 4, 7: 3, 8: 0, 9: 2, 10: 7, 11: 6, 12: 7, 13: 7, 14: 4, 15: 4, 16: 6, 17: 4, 18: 0, 19: 4, 20: 10, 21: 0, 22: 5, 23: 2}

rune_name_map = {
    1: "Energy",
    2: "A",
    3: "B",
    # And so on upto 23. You need to create this map hard-coded or read from a json
    23: "V"
}


def convert_to_rune_set(count_map, name_map):
    new_count_map = {}
    for i in range(1, 24):
        new_count_map[name_map[i]] = count_map[i]
        # name_map[i] gives the name mapped to that number
    return new_count_map


new_map = convert_to_rune_set(rune_set_counts, rune_name_map)
print(new_map)
#prints {'Energy': 0, 'A': 0, 'B': 6, 'C': 5, ......., 'V': 2}
Roy
  • 344
  • 2
  • 12
  • Thanks, that's what I was looking for. Now I know I can use dictionaries as maps. Cool! – KGE May 24 '21 at 06:45
  • Indeed. `Dictionary` is pythonic implementation of Hashmap. You can get more insights from here. https://stackoverflow.com/questions/114830/is-a-python-dictionary-an-example-of-a-hash-table – Roy May 24 '21 at 08:25
0

If ALL you want to is swap the keys and values, that is like this:

def convert_to_rune_set(old_dict, new_dict):
    for k,v in old_dict.items()
        new_dict[v] = k

However, in your description, I don't understand where "Energy" comes from. Remember, dictionaries do not have "order". You can't refer to the 3rd entry in a dictionary. Perhaps you need a list of names to keep things straight?

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • Oh, thank you, that's the problem here; I was thinking that dictionaries did have order and I could just set the value of the *first* key in new_dict to the value of the *first* key in old_dict. Would you mind telling me how I can accomplish that using a list? – KGE May 24 '21 at 05:55