Reputation: 61
so I'm getting the error: RuntimeError: dictionary changed size during iteration.
I have 2 matrixes, one for Xbox information, and one with PS4 information
The first function creates the dictionary, given the Xbox matrix. It looks at each list inside of the matrix, and takes information from each list and adds it to the dictionary. The second function takes the dictionary already made in def create_dictionary_xbox
and adds to it. I am trying to make it so that it prints out something like this:
{genre:{game:[info], game:[info]}}
Here's my code:
def create_dictionary_xbox(lists_of_data):
dictionary = {}
for list_ in lists_of_data:
game = list_[0]
genre = list_[2]
if genre not in dictionary:
dictionary[genre] = {game : list_[3:]}
elif genre in dictionary:
(dictionary[genre])[game] = list_[3:]
return dictionary
def create_dictionary_PS4(lists_of_data,dictionary):
for list_ in lists_of_data:
game = list_[0]
genre = list_[2]
for key in dictionary:
if genre not in dictionary:
dictionary[genre] = {game : list_[3:]}
elif genre in dictionary:
(dictionary[genre])[game] = list_[3:]
return dictionary
Upvotes: 0
Views: 53
Reputation: 308
I'm assuming that the data structure is like this:
['gameX', 'useless_info', 'genreX', 'info', 'info', ...]
I guess that if the data structure on both lists are the same it's easier to sum the two lists and interact only one time right?
complete_list = list_of_data1 + list_of_data2
# make one list with all the data
dict_games = {genre : {} for genre in set([x[2] for x in complete_list])}
# make a dict of dict with all genres
for game, _, genre, *info in complete_list:
if game in dict_games[genre]:
# check if the game exits on both list of data and sum the info
info = info + dict_games[genre][game]
dict_games[genre].update({game: info})
I think this is the simplest way to go in case you want to sum the info of a same game that appears in both lists. But in case you want to discard info then you can sum the list of data by priority or if you want to create some rule for discarding the info then I suggest appending a flag on the data structure and use it later when updating the dict_games. please let me know if it worked or if something isn't so clear.
Upvotes: 1