开发者

fastest way to search through this data object? (python)

开发者 https://www.devze.com 2022-12-23 16:50 出处:网络
I have a data object that looks like this: { \'node-16\': { \'tags\': [\'cuda\'], \'localNodes\': [ { \'name\': \'nC\',

I have a data object that looks like this:

    {
        'node-16': {
            'tags': ['cuda'],
            'localNodes': [
                {
                    'name': 'nC',
                    'consumesFrom': ['nA', 'nB'],
                    'classType': 'VectorAdder.VectorAdder'
                },
                {
                    'name': 'nB',
                    'consumesFrom': None,
                    'classType': 'RandomVector'
                }
            ]
        },
        'node-17': {
            'tags': ['boring'],
            'localNodes': [
  开发者_StackOverflow中文版              {
                    'name': 'nA',
                    'consumesFrom': None,
                    'classType': 'RandomVector'
                }
            ]
        }
    }

Notice that node nA is a producer for nC. What's the fastest way to find out if a given localNode is a producer for another localnode in the data structure (and not within the same list)?

For example, I would like to know that nA (node-17) produces for nC (exists on node-16). But I don't need to know that nB produces for nC, since they exist in the same localNodes list.


namedict = dict((x['name'], y) for y in data for x in data[y]['localNodes'])
proddict = dict((z['name'], [y for y in z['consumesFrom'] if namedict[y] != x])
  for x in data for z in data[x]['localNodes'] if z['consumesFrom'] is not None)

print 'nA' in proddict['nC']
0

精彩评论

暂无评论...
验证码 换一张
取 消