【发布时间】:2022-12-02 01:28:15
【问题描述】:
I have following list of dict records, where I need to extract out all the duplicates (based on the 'label') and leave one per 'label' in the original records. Also when the items get removed by 'label', always removed one with the 'headings' = True over the 'headings' = False:
Input:
records = [{"label":'x', "headings":False, "key":300}, {"label":'x', "headings":True, "key":301},
{"label":'x', "headings":False, "key":302},{"label":'x', "headings":False, "key":303},
{"label":'y', "headings":False, "key":304}, {"label":'y', "headings":True, "key":305},
{"label":'z', "headings":True, "key":306}, {"label":'z', "headings":True, "key":307}]
output
duplicate_items :
records = [{"label":'x', "headings":False, "key":300},{"label":'x', "headings":True, "key":301},{"label":'x', "headings":False, "key":302},{"label":'y', "headings":True, "key":305},{"label":'z', "headings":True, "key":306}]
【问题讨论】:
-
You must have accidentally forgotten to include your own attempt/code.
-
So. What have you tried so far ?
-
It is also strange that you consider for example
{"label":'z', "headings":True, "key":306}to be the duplicate since it has the samelabelandheadingsvalues as the next dictionary (with"key": 307). Typically I would say theduplicateis the one that comeslater. Or is there some additional rule about which dictionaries to keep/remove that you did not mention? -
Same for
{"label":'x', "headings":False, "key":300}. Why do you treat that as the duplicate and not{"label": "x", "headings": False, "key": 303}, even though the latter comes later in yourrecordslist?
标签: python list dictionary key