简体   繁体   English

如果项目中的值重复,如何从字典列表中删除字典

[英]How to remove dictionary from list of dictionary if value repeats in items

List of dictionary is below字典列表如下

l = [
{'firstname': 'joe', 'surname': 'ABCD', 'tile' : 'DE'},
{'firstname': 'john', 'surname': 'DEF', 'tile' : 'BC'},
{'firstname': 'joe', 'surname': 'bloggs', 'tile' : 'DE'},
{'firstname': 'jane', 'surname': 'HH', 'tile' : 'AD'}
]

Need to delete item from l if firstname and tile matches如果firstnametile匹配,需要从l中删除项目

pseudo code伪代码

for i in l:
   if i['firstname'] + i['tile'] in l:
        l.pop(i)
       

I have gone through python remove duplicate dictionaries from a list我已经通过python 从列表中删除重复的字典

its removing entire dict matches它删除整个字典匹配

Also gone through second answer python remove duplicate dictionaries from a list还经历了第二个答案python 从列表中删除重复的字典

Try:尝试:

l = [
    {"firstname": "joe", "surname": "ABCD", "tile": "DE"},
    {"firstname": "john", "surname": "DEF", "tile": "BC"},
    {"firstname": "joe", "surname": "bloggs", "tile": "DE"},
    {"firstname": "jane", "surname": "HH", "tile": "AD"},
]

out = {}
for d in reversed(l):
    out[(d["firstname"], d["tile"])] = d

print(list(out.values()))

Prints:印刷:

[
    {"firstname": "jane", "surname": "HH", "tile": "AD"},
    {"firstname": "joe", "surname": "ABCD", "tile": "DE"},
    {"firstname": "john", "surname": "DEF", "tile": "BC"},
]

Try this,尝试这个,

Extended this SO answer Index of duplicates items in a python list扩展了这个 SO answer Index of duplicates items in a python list

Code:代码:

from collections import defaultdict

l = [
{'firstname': 'joe', 'surname': 'ABCD', 'tile' : 'DE'},
{'firstname': 'john', 'surname': 'DEF', 'tile' : 'BC'},
{'firstname': 'joe', 'surname': 'bloggs', 'tile' : 'DE'},
{'firstname': 'jane', 'surname': 'HH', 'tile' : 'AD'}
]


group = [(item['firstname'], item['tile']) for item in l]

def list_duplicates(seq):
    tally = defaultdict(list)
    for i,item in enumerate(seq):
        tally[item].append(i)
    return ((key,locs) for key,locs in tally.items() 
                            if len(locs)>=1)

new_l = []
for dup in list_duplicates(group):
    new_l.append(l[dup[1][0]])
  
new_l

Output:输出:

[{'firstname': 'joe', 'surname': 'ABCD', 'tile': 'DE'},
 {'firstname': 'john', 'surname': 'DEF', 'tile': 'BC'},
 {'firstname': 'jane', 'surname': 'HH', 'tile': 'AD'}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对于列表中的每个值,如何从字典列表中删除项目 - How to remove items from dictionary list, for each value that's in a list 如果值重复,如何从字典中删除重复项 - How to remove duplicates from the dictionary if values repeats 如何计算列表中作为字典中的值的项目的重复次数? - How to count the number of repeats for an item in a list which is a value in a dictionary? 如何从现有列表创建字典? 键值必须是该键的重复次数 - How to create a dictionary from existing list? Key Value must be the amount of repeats for that key Python:如何遍历词典列表并将每个词典作为值添加到具有唯一键的新词典中-无需重复 - Python: How to iterate over a list of dictionaries and add each dictionary as value to a new dictionary with a unique key - no repeats Python:列表由字典项组成,如果列表中的值也从列表中删除键 - Python: list consist of dictionary items, remove key from list if in list is also it's value python-如果字典键等于某个值,则从列表中删除字典 - python - remove a dictionary from a list if dictionary key equals a certain value 如何将列表项作为值添加到字典中 - How to add the list items to a dictionary as value 如果字典包含禁止值,则从列表中删除字典 - Remove dictionary from list if it contains forbidden value 从列表中删除字典 - Remove dictionary from list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM