'Python list overwritten each time the previous items
The objective of my application is to create a copy of documents and insert them into the database for each annotator, for example, let's say the master user has 3 documents and two annotators, let's say the first annotator has the email [email protected] and the second one has the email [email protected], finally, I need to insert in the database 3 documents for [email protected] and 3 other documents for [email protected]. So to do this, I created first a list containing the copy of documents, and I didn’t assign the annotator
created_documents_in_iaa = []
created_documents_in_iaa += [DocumentIAATask(
name=document.name,
spans=document.spans,
entity=document.entity,
original_document=document,
IAA_Task=iaa_task) for document in all_documents_in_iaa.iterator(chunk_size=1)]
And after that, I added an annotator to each document, and I append each modified created_documents_in_iaa list to the tsks_list:
tasks_list = []
for user in users_instance_list:
try:
for i in range(len(created_documents_in_iaa)):
created_documents_in_iaa[i].annotator = user
tasks_list.append(created_documents_in_iaa)
except Exception as e:
print(e)
Finally, I created a copy of documents in the PostgreSQL database:
for task in tasks_list:
DocumentIAATask.objects.bulk_create(task)
However, in the result, I found 6 documents inside the table of the first annotator, and nothing for the other one, after I debugged the code, I found that tasks_list overwritten each time the list, that's means when I add the first annotator to the 3 documents, and when I did the same for the other annotator, the list overwritten the previous one, that's means, I found in the tasks_list 2 objects, each object contains 3 documents, and each document contains the same annotator, so the other one was changed after the append.
I tried to fix this problem using some similar problems that I found them on the net:
tasks_list.append(created_documents_in_iaa[:])tasks_list.append(created_documents_in_iaa.copy())
tasks_list2 += tasks_list2[:] + created_documents_in_iaa
tasks_list.append(tasks_list2)
However, it's still the same problem. Any suggestion, please?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
