>>> accounts[2]
{'title': 'Gow to get this one?'}
>>> account[10]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'account' is not defined
>>> accounts.get(2)
{'title': 'Gow to get this one?'}
>>> accounts.get(10)
# None
这将返回列表中id==2的任何元素
这似乎是一个奇怪的数据结构,但可以做到:
也许以id号为键的字典更合适,因为这样可以更容易地访问:
鉴于您的数据结构:
如果项目不存在:
也就是说,如果你有机会这样做,你可能会重新考虑你的数据结构:
然后,您可以通过索引数据的} 来直接访问数据,这取决于您希望如何处理不存在的键。
id
或使用^{相关问题 更多 >
编程相关推荐