我有一個Python中的字典,其中的鍵是路徑名。例如:在字典中打印鍵的特定子集
dict["/A"] = 0
dict["/A/B"] = 1
dict["/A/C"] = 1
dict["/X"] = 10
dict["/X/Y"] = 11
我想知道,給出任何關鍵字都打印所有「子路徑」的好方法是什麼。
例如,給定一個名爲做到這一點 「print_dict_path」 功能,如
print_dict_path("/A")
或
print_dict_path("/A/B")
東西會打印出像:
"B" = 1
"C" = 1
唯一我能想到的方法就像使用正則表達式並遍歷整個字典,但我是n不知道如果這是最好的方法(我也不是精通正則表達式)。
感謝您的任何幫助。不使用正則表達式
你可能想看到我的帖子http://stackoverflow.com/questions/3350413/is-there-a-faster-way-to-get-subtrees-from-tree-like-structures-in-python-than- th/3350642#3350642如果你認爲樹的結構。 – 2010-08-09 17:55:16