将具有相同值的字典合并到单个字典中

时间:2015-06-28 14:35:50

标签: python dictionary

我有一个字典列表:

[{'point': '-2.43896484341047, 53.4369463378926',
  'time': '2015-06-17 12:51:16+01:00',
  'title': 'Queen and Royal Family Members Visit Manchester'},
 {'point': '-0.150032043436181, 51.5402430395087',
  'time': '2015-06-20 12:52:29+01:00',
  'title': 'Price Harry Quits the Army to Concentrate on Charity Work'},
 {'point': '-0.150032043436181, 51.5402430395087',
  'time': '2015-06-26 17:01:19+01:00',
  'title': 'A true friend: Ukraine president asks Tony Blair to take on advisory role'},
 {'point': '-77.1075439345789, 35.456615048032',
  'time': '2015-06-26 17:01:19+01:00',
  'title': 'A true friend: Ukraine president asks Tony Blair to take on advisory role'}]

我希望能够搜索字典,并且在两个字典共享相同标题和时间的情况下,合并标题和时间并保留不同的点值,创建如下内容:

[{'point': '-2.43896484341047, 53.4369463378926',
  'time': '2015-06-17 12:51:16+01:00',
  'title': 'Queen and Royal Family Members Visit Manchester'},
 {'point': '-0.150032043436181, 51.5402430395087',
  'time': '2015-06-20 12:52:29+01:00',
  'title': 'Price Harry Quits the Army to Concentrate on Charity Work'},
 {'point': ['-0.150032043436181, 51.5402430395087', '-77.1075439345789, 35.456615048032'],
  'time': '2015-06-26 17:01:19+01:00',
  'title': 'A true friend: Ukraine president asks Tony Blair to take on advisory role'}]

提前感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

试试这个,抱歉我的命名惯例不好:

your_dict = [{'point': '-2.43896484341047, 53.4369463378926',
  'time': '2015-06-17 12:51:16+01:00',
  'title': 'Queen and Royal Family Members Visit Manchester'},
 {'point': '-0.150032043436181, 51.5402430395087',
  'time': '2015-06-20 12:52:29+01:00',
  'title': 'Price Harry Quits the Army to Concentrate on Charity Work'},
 {'point': '-0.150032043436181, 51.5402430395087',
  'time': '2015-06-26 17:01:19+01:00',
  'title': 'A true friend: Ukraine president asks Tony Blair to take on advisory role'},
 {'point': '-77.1075439345789, 35.456615048032',
  'time': '2015-06-26 17:01:19+01:00',
  'title': 'A true friend: Ukraine president asks Tony Blair to take on advisory role'}]

def merge_your_dictcts(x):
    dd = {'title':x[0]['title'],'time':x[0]['time']}
    points = []
    for d in x:
        points.append(d['point'])
    dd['point'] = points
    return dd
final_list = []
for k in your_dict:
    x =  [j for j in your_dict if j['time'] == k['time'] and j['title'] == k['title']]
    if len(x) >= 2:
        if merge_your_dictcts(x) not in final_list:
            final_list.append(merge_your_dictcts(x))
    else:
        final_list.append(x[0])
print(final_list)

输出:

[{
    'title': 'Queen and Royal Family Members Visit Manchester',
    'time': '2015-06-17 12:51:16+01:00',
    'point': '-2.43896484341047, 53.4369463378926'
}, {
    'title': 'Price Harry Quits the Army to Concentrate on Charity Work',
    'time': '2015-06-20 12:52:29+01:00',
    'point': '-0.150032043436181, 51.5402430395087'
}, {
    'point': ['-0.150032043436181, 51.5402430395087', '-77.1075439345789, 35.456615048032'],
    'time': '2015-06-26 17:01:19+01:00',
    'title': 'A true friend: Ukraine president asks Tony Blair to take on advisory role'
}]

答案 1 :(得分:1)

尝试这个更短的解决方案:

dicts = [
 {'point': '-2.43896484341047, 53.4369463378926',
  'time': '2015-06-17 12:51:16+01:00',
  'title': 'Queen and Royal Family Members Visit Manchester'},
 {'point': '-0.150032043436181, 51.5402430395087',
  'time': '2015-06-20 12:52:29+01:00',
  'title': 'Price Harry Quits the Army to Concentrate on Charity Work'},
 {'point': '-0.150032043436181, 51.5402430395087',
  'time': '2015-06-26 17:01:19+01:00',
  'title': 'A true friend: Ukraine president asks Tony Blair to take on advisory role'},
 {'point': '-77.1075439345789, 35.456615048032',
  'time': '2015-06-26 17:01:19+01:00',
  'title': 'A true friend: Ukraine president asks Tony Blair to take on advisory role'}]

ans = []
for time, title in set((d['time'], d['title']) for d in dicts):
    points = [d['point'] for d in dicts if (d['time'], d['title']) == (time, title)]
    ans.append({
        'point' : points if len(points) > 1 else points[0],
        'time'  : time,
        'title' : title })

结果存储在ans变量中并具有预期的结构 - 尽管输出列表中的元素可能以不同的顺序出现,因为我使用set来查找独特的“钥匙”:

[{'title': 'A true friend: Ukraine president asks Tony Blair to take on advisory role',
  'time': '2015-06-26 17:01:19+01:00',
  'point': ['-0.150032043436181, 51.5402430395087', '-77.1075439345789, 35.456615048032']},
 {'title': 'Price Harry Quits the Army to Concentrate on Charity Work',
  'time': '2015-06-20 12:52:29+01:00',
  'point': '-0.150032043436181, 51.5402430395087'},
 {'title': 'Queen and Royal Family Members Visit Manchester',
  'time': '2015-06-17 12:51:16+01:00',
  'point': '-2.43896484341047, 53.4369463378926'}]

答案 2 :(得分:0)

制作一个通道来收集重复的文章,然后通过第二个通道发出如您所述的分组文章:

# Gather duplicate articles
mergedict = {}
for article in news:
    key = article['time'], article['title']
    value = article['point']
    mergedict.setdefault(key, []).append(value)

# Format output
result = []
for (time, title), points in mergedict.items():
    points = points if len(points) > 1 else points[0]
    article = {'time': time, 'title': title, 'points': points}
    result.append(article)