简体   繁体   中英

push values from a python dictionary to corresponding key-values of a second dictionary

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?

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}

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM