Python字典动态更新

时间:2013-11-08 15:36:02

标签: python dictionary

所以我有这个字典x从这个函数

创建
    olist = ['a/b/c','a/b/c/d','b/c/d','x/y/z','a/b','d/e/f','i/j/k']

    def bulkFeed(objlist):
        x = {}
        for obj in objlist:
            #pdb.set_trace()
            dn_len = len(obj)
            if dn_len not in x:
                x[dn_len] = {}
            if obj not in x[dn_len]:
                x[dn_len][obj] = {}
            x[dn_len][obj].update({"commit":1,"ObjectRef":obj})
        return x

obj_dict = bulkFeed(olist)

 >>> obj_dict
    {3: {'a/b': {'commit': 1, 'ObjectRef': 'a/b'}}, 5: {'a/b/c': {'commit': 1, 'ObjectRef': 'a/b/c'}, 'd/e/f': {'commit': 1, 'ObjectRef': 'd/e/f'}, 'b/c/d': {'commit': 1, 'ObjectRef': 'b/c/d'}, 'x/y/z': {'commit': 1, 'ObjectRef': 'x/y/z'}, 'i/j/k': {'commit': 1, 'ObjectRef': 'i/j/k'}}, 7: {'a/b/c/d': {'commit': 1, 'ObjectRef': 'a/b/c/d'}}}

我想要做的是在obj_dict

中这样的事情
if 'a/b/c'.startswith('a/b'):
    update commit to 0 in 'a/b/c' level
if  'a/b/c/d'.startswith('a/b/c'):
    update commit to 0 in 'a/b/c/d' level

keep doing for every item in dictionary until reaching end of dict

谢谢任何答案/抬头将非常有帮助。 'a / b / c'源于内部词典级别,例如'a / b / c / d'词干

0 个答案:

没有答案