Note that there are some explanatory texts on larger screens.

plurals
  1. POappend() algorithm incorrectly appends
    text
    copied!<p>I have the following code: </p> <pre><code>sportMappings = {"sport1" : {"file" : "sport1.json", "label" : "Sport 1"},"sport2" : {"file" : "sport2.json", "label" : "Sport 2"},"sport3" : {"file" : "sport3.json", "label" : "Sport 3"}} normalizedEntries = {"o.json" : {"spotlight" : []}} normalizedEntries = createList(normalizedEntries, map_key, sportMappings) def createList(normalizedEntries, key, sportMappings): for range in SOURCES["ranges"]: data = read_files.readJSONUrl(logger, SOURCES[key] + str(range)) for entry in data["entries"]: normalizedEntry = normalize(entry, "type") if normalizedEntry is not None: #append to a catchall dict normalizedEntries["o.json"]["spotlight"].append(normalizedEntry) if normalizedEntry["league"] in sportMappings: if sportMappings[normalizedEntry["league"]]["file"] not in normalizedEntries: normalizedEntries[sportMappings[normalizedEntry["league"]]["file"]] = {"spotlight" : []} #append to the specific league to which that sport belongs...ie sport1, sport2 normalizedEntries[sportMappings[normalizedEntry["league"]]["file"]]["spotlight"].append(normalizedEntry) else: pass return normalizedEntries </code></pre> <p>The point of which is to get a json input, iterate over each object in the json, normalize its format, then append ALL objects to o.json and then append the entry to its corresponding sport file from the sportMappings dict. </p> <p>I expect that after iteration 1 normalizedEntries' contents will be : </p> <pre><code>--o.json --entryForSport2 --sport1.json --sport2.json --entryForSport2 --sport3.json </code></pre> <p>After iteration 2 normalizedEntries' contents will be: </p> <pre><code> --o.json --entryForSport2 --entryForSport1 --sport1.json --entryForSport1 --sport2.json --entryForSport2 --sport3.json </code></pre> <p>And so on. </p> <p>Instead, after the function runs completely, all sub dictionaries are all the same. </p> <pre><code>--o.json --entryForSport2 --entryForSport1 --sport1.json --entryForSport2 --entryForSport1 --sport2.json --entryForSport2 --entryForSport1 --sport3.json --entryForSport2 --entryForSport1 </code></pre> <p><strong>Question: Why are the items being appended to ALL sub dictionaries for each iteration as opposed to each item going into the catch-all o.json and then to its appropriate sport subdict?</strong> note - "spotlight is a list, not a dict so append() should be the appropriate tool, yes?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload