我試圖從字典中的某個點拆分字典。這似乎是做一個簡單的items_dict[3:]
將工作,但它沒有奏效。從某個鍵分割一個python字典
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並不是一個好主意。 –