So I have a nested dictionary:
{1: {'letter': 'A', 'is_vowel': True, 'point_value': 1},
2: {'letter': 'B', 'is_vowel': False, 'point_value': 3},
3: {'letter': 'C', 'is_vowel': False, 'point_value': 3},
4: {'letter': 'D', 'is_vowel': False, 'point_value': 2},
5: {'letter': 'E', 'is_vowel': True, 'point_value': 1},
6: {'letter': 'F', 'is_vowel': False, 'point_value': 4},
7: {'letter': 'G', 'is_vowel': False, 'point_value': 2},
8: {'letter': 'H', 'is_vowel': False, 'point_value': 4},
9: {'letter': 'I', 'is_vowel': True, 'point_value': 1},
10: {'letter': 'J', 'is_vowel': False, 'point_value': 8},
11: {'letter': 'K', 'is_vowel': False, 'point_value': 5},
12: {'letter': 'L', 'is_vowel': False, 'point_value': 1},
13: {'letter': 'M', 'is_vowel': False, 'point_value': 3},
14: {'letter': 'N', 'is_vowel': False, 'point_value': 1},
15: {'letter': 'O', 'is_vowel': True, 'point_value': 1},
16: {'letter': 'P', 'is_vowel': False, 'point_value': 3},
17: {'letter': 'Q', 'is_vowel': False, 'point_value': 10},
18: {'letter': 'R', 'is_vowel': False, 'point_value': 1},
19: {'letter': 'S', 'is_vowel': False, 'point_value': 1},
20: {'letter': 'T', 'is_vowel': False, 'point_value': 1},
21: {'letter': 'U', 'is_vowel': True, 'point_value': 1},
22: {'letter': 'V', 'is_vowel': False, 'point_value': 4},
23: {'letter': 'W', 'is_vowel': False, 'point_value': 4},
24: {'letter': 'X', 'is_vowel': False, 'point_value': 8},
25: {'letter': 'Y', 'is_vowel': False, 'point_value': 4},
26: {'letter': 'Z', 'is_vowel': False, 'point_value': 10}
}
I would like to use a list of random letters for example:
['I', 'O', 'N', 'V', 'X', 'V', 'H', 'L', 'O', 'N', 'P', 'A', 'L', 'R', 'G', 'G', 'N', 'K', 'W', 'L', 'I', 'P', 'R', 'X', 'R', 'A']
and make a NEW list that contains the corresponding point_value listed in the dictionary for each letter.
What's the best way to go about this?
I tried making a function that takes the list and dictionary as arguments:
def point_value_rand_letters(list, dic):
point_values = []
for letter in list:
value = {i for i in dic if dic[i]['letter']==letter}
point_values.append(value)
print(point_values)
return point_values
But I think I'm misunderstanding the code I used to find the value:
{i for i in dic if dic[i]['letter']==letter}
doing this gives me a list of the keys but as a list of sets (this is just an example and these don't correspond to the example list I made above):
[{12}, {19}, {20}, {24}, {23}, {24}, {15}, {8}, {21}, {7}, {19}, {14}, {9}, {15}, {9}, {6}, {19}, {9}, {22}, {17}, {11}, {15}, {19}, {2}, {4}]
Maybe I need to convert the sets to an int somehow? and then I can use those as keys to find 'point_value'? Or maybe I'm being too complicated and approaching this wrong...
Thanks!
Iterate over each element in the list given (i.e. letters), find its key using ord()
, get the key's corresponding value and append that to the new list:
def point_value_rand_letters(letter_list, dict_):
point_values = []
for letter in letter_list:
value = dict_[ord(letter) - ord('A') + 1]['point_value']
point_values.append(value)
return point_values
Try it:
l = [
'I', 'O', 'N', 'V', 'X', 'V', 'H', 'L', 'O',
'N', 'P', 'A', 'L', 'R', 'G', 'G', 'N', 'K',
'W', 'L', 'I', 'P', 'R', 'X', 'R', 'A'
]
print(point_values_rand_letters(l, your_dict))
# [
# 1, 1, 1, 4, 8, 4, 4, 1, 1,
# 1, 3, 1, 1, 1, 2, 2, 1, 5,
# 4, 1, 1, 3, 1, 8, 1, 1
# ]