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

2024-06-26 18:07:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一份字典清单,所以:

[{'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'}]

提前感谢您的帮助!你知道吗


Tags: tofriendtrue字典timetitleonpoint
3条回答

进行一次传递以收集重复的文章,第二次传递以发出分组的文章,如您所述:

# 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)

尝试以下较短的解决方案:

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'}]

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

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'
}]

相关问题 更多 >