Python dictionaries list

I have the following entry for a list of dictionaries:

    links = [ {'uid': 1, 'lid': 6, 'path': 'a1.txt', 'shareid': 1},
      {'uid': 1, 'lid': 7, 'path': 'a2.txt', 'shareid': 2},
      {'uid': 1, 'lid': 8, 'path': 'a1.txt', 'shareid': 1}]

and I need to generate this output:

    op = {'a1.txt': {'shareid': 1, 'lid': [6, 8]},
          'a2.txt': {'shareid': 2, 'lid': [7]}
         }

Below is the code I wrote:

def list_all_links():
       new_list = []
       result = {}

       for i in range(len(links)):
           entry = links[i]

    if not result.has_key(entry['path']):
        new_entry = {}
        lid_list = []
        new_entry['shareid'] = entry['shareid']
        if new_entry.has_key('lid'):
            lid_list = new_entry['lid']
            lid_list.append(entry['lid'])
        else:
            lid_list.append(entry['lid'])
        new_entry['lid'] = lid_list

        result[entry['path']] = new_entry

    else:
        new_entry = result[entry['path']]
        lid_list = new_entry['lid']

        if new_entry.has_key(entry['shareid']):
            new_entry['shareid'] = entry['shareid']
            lid_list = new_entry['lid']
            lid_list.append(entry['lid'])
            new_entry['lid'] = lid_list


        else:
            new_entry['shareid'] = entry['shareid']
            lid_list.append(entry['lid'])
            new_entry['lid'] = lid_list


        result[entry['path']] = new_entry

print "result = %s" %result


if __name__ == '__main__':
    list_all_links()

I can generate the same result as you. But can someone please tell me if there is a better way to solve this problem?

+4
source share
4 answers

You can use the setdefaultmethod dictto make it short.

links = [
  {'uid': 1, 'lid': 6, 'path': 'a1.txt', 'shareid': 1},
  {'uid': 1, 'lid': 7, 'path': 'a2.txt', 'shareid': 2},
  {'uid': 1, 'lid': 8, 'path': 'a1.txt', 'shareid': 1}
]

op = dict()
for a in links:
  op.setdefault(a['path'], {}).update(shareid=a['shareid'])
  op[a['path']].setdefault('lid', []).append(a['lid'])
print op

Output:

{'a2.txt': {'lid': [7], 'shareid': 2}, 'a1.txt': {'lid': [6, 8], 'shareid': 1}}
0
source

This is not all so beautiful, but the following solution works:

links = [ {'uid': 1, 'lid': 6, 'path': 'a1.txt', 'shareid': 1},
      {'uid': 1, 'lid': 7, 'path': 'a2.txt', 'shareid': 2},
      {'uid': 1, 'lid': 8, 'path': 'a1.txt', 'shareid': 1}]

links_restructured = [(d['path'], {'shareid': d['shareid'], 'lid': [d['lid']]}) for d in links]
answer = {}
for link in links_restructured:
    if link[0] not in answer:
        answer[link[0]] = link[1]
    else:
        answer[link[0]]['lid'].extend(link[1]['lid'])
print(answer)

Exit

{'a2.txt': {'lid': [7], 'shareid': 2}, 'a1.txt': {'lid': [6, 8], 'shareid': 1}}
0
source
links = [ {'uid': 1, 'lid': 6, 'path': 'a1.txt', 'shareid': 1},
      {'uid': 1, 'lid': 7, 'path': 'a2.txt', 'shareid': 2},
      {'uid': 1, 'lid': 8, 'path': 'a1.txt', 'shareid': 1}]


def get_links(links):
    new_links = {}
    for x in links:
        path = x.get('path')
        if path in new_links.keys():
            new_links[path]['lid'].append(x['lid'])
        else:
            del x['path']
            del x['uid']
            x['lid'] = [x['lid']]
            new_links[path] = x

    return new_links

print(get_links(links))

:

{'a2.txt': {'lid': [7], 'shareid': 2}, 'a1.txt': {'lid': [6, 8], 'shareid': 1}}
0

:

def process_links(links):
    '''
       process entries in list 'links';
       returns dictionary 'op'
    '''
    op = {}
    for dict in links:
        op_key = dict['path']
        if op_key in op:
            pass
        else:
            op[op_key] = {'shareid':None, 'lid':[]}
    return op

def fill_op(op_dict, link_list):
    for dict in link_list:
        op_key = dict['path']
        # fill shareid
        op_dict[op_key]['shareid'] = dict['shareid']
        # fill lid
        lid_list = op_dict[op_key]['lid'] 
        lid_list.append(dict['lid'])
        op_dict[op_key]['lid'] = lid_list
    return op_dict


if __name__ == "__main__":
    links = [ {'uid': 1, 'lid': 6, 'path': 'a1.txt', 'shareid': 1},
              {'uid': 1, 'lid': 7, 'path': 'a2.txt', 'shareid': 2},
              {'uid': 1, 'lid': 8, 'path': 'a1.txt', 'shareid': 1}]
    result1 = process_links(links)
    result2 = fill_op(result1, links)
    print(result2)

: {'a1.txt': {'lid': [6, 8], 'shareid': 1}, 'a2.txt': {'lid': [7], 'shareid': 2}}

0

Source: https://habr.com/ru/post/1623938/


All Articles