需要从词典列表中删除重复项,并更改其余重复项的数据(python)

考虑以下简短的python字典列表(第一个字典项是一个字符串,第二个项是Widget对象):

raw_results =  
     [{'src': 'tag', 'widget': <Widget: to complete a form today>},   # dupe 1a
      {'src': 'tag', 'widget': <Widget: a newspaper>},                # dupe 2a
      {'src': 'zip', 'widget': <Widget: to complete a form today>},   # dupe 1b
      {'src': 'zip', 'widget': <Widget: the new Jack Johnson album>},
      {'src': 'zip', 'widget': <Widget: a newspaper>},                # dupe 2b
      {'src': 'zip', 'widget': <Widget: premium dog food >}]

我想浏览该列表并删除重复项,因此SO问题为我解答了这些重复项:

Remove duplicates in a list while keeping its order (Python)

    known_widgets= set()
    processed_results = []

    for x in raw_results:
        widget = x['widget']
        if widget in known_widgets: 
            continue
        else:
            processed_results.append(x)
            known_widgets.add(widget)

但是,删除重复行(例如重复1b)后,我想更改其余重复的(例如重复1a)“ src”数据.我想将已删除的重复项“ src”附加到原始文件中.这就是我想要的最终结果:

processed_results =  
    [{'src': 'tag-zip', 'widget': <Widget: to complete a form today>},  # dupe 1a
     {'src': 'tag-zip', 'widget': <Widget: a newspaper>},               # dupe 2a
     {'src': 'zip', 'widget': <Widget: the new Jack Johnson album>},
     {'src': 'zip', 'widget': <Widget: premium dog food >}]

我敢肯定,这很容易做到,但是在喝了太多咖啡并且绕了好几个小时之后,我的头开始旋转.我很乐意并且非常感谢专家的帮助.谢谢!

解决方法:

def find_widget(widget, L):
    for i, v in enumerate(L):
      if v[widget] == widget:
          return i

known_widgets= set()
processed_results = []

for x in raw_results:
    widget = x['widget']
    if widget in known_widgets:
        processed_widgets[find_widget(widget, processed_results)]['src'] += '-%s' % x['tag']
        continue
    else:
        processed_results.append(x)
        known_widgets.add(widget)

可能会做得更好(因为对于每个重复的小部件这是两次传递).

上一篇:442. Find All Duplicates in an Array


下一篇:python-从numpy数组中删除出现多次的元素