List.append () replaces each variable with a new one.
I have a loop in which I edit a json object and add it to a list. But outside of the loop, the value of all old items will be changed to the new one.
My question is similar to this one here, but I still cannot find a solution to my problem.
This is my code:
json_data = open(filepath).read()
data = json.loads(json_data)
dataNew=[]
#opening file to write json
with open(filepath2, 'w') as outfile:
for i in range(50):
random_index_IntentNames = randint(0,len(intent_names)-1)
random_index_SessionIds = randint(0,len(session_id)-1)
timestamp = strftime("%Y-%m-%d %H:%M:%S", gmtime())
data["result"]["metadata"]["intentName"] = intent_names[random_index_IntentNames]
data["sessionId"]=session_id[random_index_SessionIds]
data["timestamp"] = timestamp
dataNew.append(data)
json.dump(dataNew, outfile, indent=2)
source to share
I was able to find a solution on my own. I gave a "data" job in a loop and it worked:
json_data = open(filepath).read()
dataNew=[]
#opening file to write json
with open(filepath2, 'w') as outfile:
for i in range(50):
random_index_IntentNames = randint(0,len(intent_names)-1)
random_index_SessionIds = randint(0,len(session_id)-1)
timestamp = strftime("%Y-%m-%d %H:%M:%S", gmtime())
data = json.loads(json_data)
data["result"]["metadata"]["intentName"] = intent_names[random_index_IntentNames]
data["sessionId"]=session_id[random_index_SessionIds]
data["timestamp"] = timestamp
dataNew.append(data)
json.dump(dataNew, outfile, indent=2)
source to share
Each item in your list is just a reference to one object in memory. Similar to what was posted in your linked answer, you need to add copies of the dict.
import copy
my_list = []
a = {1: 2, 3: 4}
b = a # Referencing the same object
c = copy.copy(a) # Creating a different object
my_list.append(a)
my_list.append(b)
my_list.append(c)
a[1] = 'hi' # Modify the dict, which will change both a and b, but not c
print my_list
You might be wondering Is Python the default or the call? Also. for further reading.
source to share
data
is a dict which means mutable and this value is passed by reference, you should use [copy.deepcopy ()] ( https://docs.python.org/2/library/copy.html#copy.deepcopy ) if you want the original data not to be disabled:
from copy import deepcopy
json_data = open(filepath).read()
data = json.loads(json_data)
dataNew=[]
#opening file to write json
with open(filepath2, 'w') as outfile:
for i in range(50):
random_index_IntentNames = randint(0,len(intent_names)-1)
random_index_SessionIds = randint(0,len(session_id)-1)
timestamp = strftime("%Y-%m-%d %H:%M:%S", gmtime())
# Create a shallow copy, modify it and append to new
new_data = deepcopy(data)
new_data["result"]["metadata"]["intentName"] = intent_names[random_index_IntentNames]
new_data["sessionId"]=session_id[random_index_SessionIds]
new_data["timestamp"] = timestamp
dataNew.append(new_data)
json.dump(dataNew, outfile, indent=2)
NOTE: If you data
don't store the changed items, you can use dict.copy to avoid changing the source value.
Luck!
source to share