假设我有一个具有以下定义的项目:
Item(id: str, sequence: int)
id是一个随机字符串。
sequence是表示将其Item
放入数据结构中的顺序的数字。
我想将所有Item
对象组织在某种数据结构中,该结构Item
基于其sequence
. 所以我使用了一个SortedKeyList,它们的键设置为sequence
.
import sortedcontainers
items = sortedcontainers.SortedKeyList(key=lambda x: x.sequence)
对于大多数操作,这可以正常工作。但问题是我有一个操作需要Item
使用特定的id
,但是没有办法使用上面的键来做到这一点。
我想做类似的事情:
items.add(Item('abc', 0))
items.add(Item('www', 1))
items.add(Item('zyx', 2))
# This should be the 2nd item added.
item = items['www']
附加信息:
我打算添加的操作需要能够Item
从特定的id
.
load_n_items_from_id(num: int, id: str) -> [Item]
使用上面的示例,此方法应返回以下结果:
loaded = load_n_items_from_id(2, 'www')
# loaded should contain [Item('www', 1), Item('zyx', 2)]