Home > Net >  Storing value in a dictionary
Storing value in a dictionary

Time:12-11

I'm working with a dictionary that will store data for a library called calendar. The library will have a format as follow

{day : [log_book, [borrow_info], [return_info], [add_book], [fine]]}

and

log_book = [[book_name], [quantity], [restricted_type]]

I intended to have each day storing a unique value into log_book. I attempted to do it by making this function

def update_storage(calendar, day, log_book):

    data = calendar.get(day)
    data[0] = log_book
    calendar.update({day:data})

    return calendar

However, as far as I understand, this creates a pointer instead of a unique value for each day. This is the result I wanted

{0 : [[['Introduction to python', 'harry potter'], [3, 1], ['TRUE', 'FALSE']], [], [], [], []],
 1 : [[['Introduction to python', 'harry potter'], [3, 0], ['TRUE', 'FALSE']], [['1', 'adam', 'harry potter', '6']], [], [], []], 
 2 : [[['Introduction to python', 'harry potter'], [3, 0], ['TRUE', 'FALSE']], [], [], [], []],
 3 : [[['Introduction to python', 'harry potter'], [3, 0], ['TRUE', 'FALSE']], [], [], [], []]}

This is the result that I currently get even though I did not update day 1 (key = 0) at all

{0 : [[['Introduction to python', 'harry potter'], [3, 0], ['TRUE', 'FALSE']], [], [], [], []],
 1 : [[['Introduction to python', 'harry potter'], [3, 0], ['TRUE', 'FALSE']], [['1', 'adam', 'harry potter', '6']], [], [], []]}
 2 : [[['Introduction to python', 'harry potter'], [3, 0], ['TRUE', 'FALSE']], [], [], [], []], 
 3 : [[['Introduction to python', 'harry potter'], [3, 0], ['TRUE', 'FALSE']], [], [], [], []]}

As you can see on day 2 (key = 1), adam borrowed a book named "harry potter" which decrease the quantity of the book down from 1 to 0. However, when I updated the dictionary, it changes every value to the current book_availability . This is an example how I updated the value using the function mentioned above

book_availability = [['Introduction to python', 'harry potter'], [3, 0], ['TRUE', 'FALSE']]
calendar = update_storage(calendar, day, book_availability)

What did I do wrong? I'm new to data structure and pointers.

CodePudding user response:

You can see if something like this work in your expected / desired way:

def update_storage(calendar, day, log_book):

    data = calendar.get(day)
    data[0] = log_book.copy()
    data[0][1] = data[0][1].copy()
    calendar.update({day:data})

    return calendar

I don't know how is your whole program like, but it seems that you are manipulating the same (set of) list(s) (of "literals").

This makes data[0] points to book_availability (which consists of "pointers" to a bunch of lists) when it's passed as log_book:

data[0] = log_book

which means each day shares the same log_book.

This will give each day a separate log_book:

data[0] = log_book.copy()

BUT, since .copy() does only a shallow copy, which means each pointer in the copy will still be pointing to the same thing (list).

Therefore, to allow each day to have its own record of "quantities", you will at least need:

data[0] = log_book.copy()
data[0][1] = data[0][1].copy()

Note that data[0][1] = data[0][1].copy() alone is not sufficient either, because if it were used with data[0] = log_book, you would be making one same log_book shared by all the days point to a new copy of "quantity" (that had / will have the "literals" in it updated).

P.S. What I wrote serves only as an explanation to your doubt. It's probably a bad way to achieve your goal in reality.

  • Related