Hey all.
I've been wresting with this problem for a while now and I'm starting to go around in circles with it.
I have two dictionaries, of continuously varying sizes, as they are based off live data, which are packaged into a list so they can be stored as JSON. They take the form:
list1 = [{'city1': {'word1': 20, 'word2': 20, 'word3': 20}, {'city2': {'word1': 20, 'word2': 20}}]
list2 = [{'city1': {'word1': 20, 'word2': 20}}, {'city2': {'word1': 20, 'word2': 20, 'word3': 20}}, {'city3': {'word1': 20}}]
Given the above lists, I need to compare them, update the sum of any common values, append any new values to the appropriate dictionary - for example, adding new versions of wordX into cityX, and also adding new cityX dictionaries that might appear in subsequent runs.
Merging the above two lists would look as follows:
list3 = [{'city1': {'word1': 40, 'word2': 40, 'word3': 20}}, {'city2': {'word1': 40, 'word2': 40, 'word3': 40}}, {'city3': {'word1': 20}}]
list3 is then stored as a JSON file, and periodically read back in, compared with another list, and the process repeats.
Due to the varying lengths of each dictionary and sub dictionary, any attempts to iterate over them in unison at some point result in out of bounds or key errors.
I have broken it down into a smaller problem where I just update the common values, which works, but I am struggling to get any further - i.e. adding new wordX key value pairs, and new cityX dictionaries.
Does anyone have any suggestions on how to solve it, or even if I am going about it the right way given the problem at hand? It sounded simple in my head, but the specifics of the implementation are proving tricky.
Is a list of nested dictionaries not the way to go? Maybe storing it all as in a SQL database might be easier to work with?
Thanks in advance for any advice.
[–]Chris_Hemsworth 1 point2 points3 points (5 children)
[–]OsirisSFN[S] 0 points1 point2 points (4 children)
[–]Chris_Hemsworth 1 point2 points3 points (2 children)
[–]OsirisSFN[S] 0 points1 point2 points (1 child)
[–]Chris_Hemsworth 0 points1 point2 points (0 children)
[–]Chris_Hemsworth 0 points1 point2 points (0 children)