【发布时间】:2013-05-14 13:23:32
【问题描述】:
我正在尝试从字典中的某个点拆分字典。似乎做一个简单的items_dict[3:] 会起作用,但它不起作用。
items_dict = {
"Cannon barrels":10,
"Cannon furnace":12,
"Candle":36,
"Bronze arrowheads":39,
"Iron arrowheads":40,
"Steel arrowheads":41,
"Mithril arrowheads":42,
"Adamant arrowheads":4
}
print items_dict[3:] # Nope, this won't work
print items_dict["Candle"] # This will of course, but only returns the Candle's number
我只知道如何to slice a dictionary by keys that start with a certain string,但我只是想知道如何将一个类似于列表的字典切片。
【问题讨论】:
-
看起来你应该重新考虑你的数据结构。我认为在这里使用 OrderedDict 不是一个好主意。
标签: python dictionary slice