我有一个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