【发布时间】:2021-12-10 07:30:04
【问题描述】:
我正在使用一个字典来存储名为calendar 的库的数据。该库将具有如下格式
{day : [log_book, [borrow_info], [return_info], [add_book], [fine]]}
和
log_book = [[book_name], [quantity], [restricted_type]]
我打算每天将一个唯一值存储到 log_book。我试图通过制作这个功能来做到这一点
def update_storage(calendar, day, log_book):
data = calendar.get(day)
data[0] = log_book
calendar.update({day:data})
return calendar
但是,据我了解,这会为每一天创建一个指针而不是唯一值。 这就是我想要的结果
{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']], [], [], [], []]}
这是我目前得到的结果,即使我根本没有更新第 1 天(key = 0)
{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']], [], [], [], []]}
正如您在第 2 天(key = 1)看到的那样,亚当借了一本名为 "harry potter" 的书,这本书的数量从 1 减少到 0。但是,当我更新字典时,它会将每个值更改为当前book_availability。这是我如何使用上述函数更新值的示例
book_availability = [['Introduction to python', 'harry potter'], [3, 0], ['TRUE', 'FALSE']]
calendar = update_storage(calendar, day, book_availability)
我做错了什么?我是数据结构和指针的新手。
【问题讨论】:
-
描述似乎不清楚。请提供测试数据、您当前的结果以及您打算拥有的结果。
-
嗨!谢谢你指出。我刚刚编辑了包含更多信息的帖子。
-
听起来问题是您实际上没有每个“天”的“数量”(也许是(“受限类型”))的单独副本
标签: python dictionary pointers