我有以下输入的词典列表:
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 = {'a1.txt': {'shareid': 1, 'lid': [6, 8]},
'a2.txt': {'shareid': 2, 'lid': [7]}
}
以下是我写的代码:
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()
我能够根据需要生成相同的输出。但是,有人可以指出我是否有更好的方法来解决这个问题?
答案 0 :(得分:0)
它不是那么漂亮,但以下解决方案有效:
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)
<强>输出强>
{'a2.txt': {'lid': [7], 'shareid': 2}, 'a1.txt': {'lid': [6, 8], 'shareid': 1}}
答案 1 :(得分:0)
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}}
答案 2 :(得分:0)
您可以使用setdefault
的{{1}}方法缩短
dict
<强>输出:强>
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
答案 3 :(得分: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)
输出略有不同:{&#39; a1.txt&#39;:{&#39; lid&#39;:[6,8],&#39; shareid&#39;:1},&#39 ; a2.txt&#39;:{&#39; lid&#39;:[7],&#39; shareid&#39;:2}}