Note that there are some explanatory texts on larger screens.

plurals
  1. POPython: Dictionary-Deleting keys having default values
    primarykey
    data
    text
    <p>I am working on a dataset and for the same a lot of data being processed is stored in dictionary. A lot many of the keys are getting updated as data is getting processed.</p> <p>To avoid checking the existence of keys at every level I have opted to create the structure in a static manner where I load the values of the key with default values.</p> <p>Example:</p> <pre><code>Student : { "Name" : "", "Exam" : { "Math" : {"Marks_Obtained" : 0, "Total_Question" : 100, .... some more keys } } } </code></pre> <p>Now when I encounter another exam stat say "English" to avoid the check operation of whether the key exists I preconfigure the structure with default values. So now my struct will look somethg like this.</p> <pre><code>Student : { "Name" : "", "Exam" : { "Math" : {"Marks_Obtained" : 0, "Total_Question" : 100, .... some more keys }, "Eng" : {"Marks_Obtained" : 0, "Total_Question" : 100, .... some more keys }, "Hist" : {"Marks_Obtained" : 0, "Total_Question" : 100, .... some more keys }, "Bio" : {"Marks_Obtained" : 0, "Total_Question" : 100, .... some more keys } } } </code></pre> <p>Now some students might not opt for an exam. I want to delete that entire structure. Say the student didnt opt for History I want to delete (del["Exam"]["Hist"]).</p> <p>I dont want to dynamically generate the structure as values are getting updated quite frequently for the exam the user gave, and checking the validity of the structure for every iteration would mean an additional O(1) process.</p> <p>What I wish to do is efficiently delete the keys which were not used. I dont want to loop using a for loop as that would be again be an inefficient way. </p> <p>If there are any doubts feel free to comment.</p> <p>Let me know your thoughts! </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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