简体   繁体   English

从字典列表中删除所有元素,并重复一个(键,值)

[英]Remove all elements from list of dictionaries with one (key, value) repeated

i have this list of dictionaries, and i want to remove those dictionaries with same genre until have only one, for example if i have two dictionaries with same genre ( Kids) i want only the first one, and the others, should be removed. 我有此字典列表,我想删除那些具有相同体裁的字典,直到只有一个,例如,如果我有两个具有相同体裁的字典(儿童),我只希望删除第一个,而其他则应删除。


Source 资源

[
     {'genre': 'Kids', 'jpg': 'as.jpg', 'file': '01-26'},
     {'genre': 'Kids', 'jpg': 'la.jpg', 'file': '02-02'},
     {'genre': 'Action', 'jpg': 'na.jpg', 'file': '01-41'},
     {'genre': 'Action', 'jpg': 'lo.jpg', 'file': '00-17'}, 
     {'genre': 'Drama', 'jpg': 'do.jpg', 'file': '01-54'}
]

Output: 输出:

[
     {'genre': 'Kids', 'jpg': 'as.jpg', 'file': '01-26'},
     {'genre': 'Action', 'jpg': 'na.jpg', 'file': '01-41'},
     {'genre': 'Drama', 'jpg': 'do.jpg', 'file': '01-54'}
]

You could do: 您可以这样做:

data = [
     {'genre': 'Kids', 'jpg': 'as.jpg', 'file': '01-26'},
     {'genre': 'Kids', 'jpg': 'la.jpg', 'file': '02-02'},
     {'genre': 'Action', 'jpg': 'na.jpg', 'file': '01-41'},
     {'genre': 'Action', 'jpg': 'lo.jpg', 'file': '00-17'},
     {'genre': 'Drama', 'jpg': 'do.jpg', 'file': '01-54'}
]


seen = set()
result = []
for e in data:
    if e['genre'] not in seen:
        seen.add(e['genre'])
        result.append(e)

print(result)

Output 输出量

[{'file': '01-26', 'jpg': 'as.jpg', 'genre': 'Kids'}, {'file': '01-41', 'jpg': 'na.jpg', 'genre': 'Action'}, {'file': '01-54', 'jpg': 'do.jpg', 'genre': 'Drama'}]

Suppose your list is l . 假设您的清单是l Just scan the list by the end, and stock items in a dictionary with genre as keys. 只需扫描列表的末尾,然后将genre为键的字典中的项目存储在字典中即可。 Only the first of each genre will stay, hiding the others. 每个类型中只有第一个类型会保留,其他类型则隐藏。 Then you just have to drop the key, keeping the values. 然后,您只需要放下键并保留值即可。

stock = {d['genre']:d for d in reversed (l)}
print( [v for v in stock.values()] )

result: 结果:

[{'genre': 'Drama', 'jpg': 'do.jpg', 'file': '01-54'}, 
{'genre': 'Action', 'jpg': 'na.jpg', 'file': '01-41'}, 
{'genre': 'Kids', 'jpg': 'as.jpg', 'file': '01-26'}]

As usual there are multiple ways: 与往常一样,有多种方法:

data = [
 {'genre': 'Kids', 'jpg': 'as.jpg', 'file': '01-26'},
 {'genre': 'Kids', 'jpg': 'la.jpg', 'file': '02-02'},
 {'genre': 'Action', 'jpg': 'na.jpg', 'file': '01-41'},
 {'genre': 'Action', 'jpg': 'lo.jpg', 'file': '00-17'}, 
 {'genre': 'Drama', 'jpg': 'do.jpg', 'file': '01-54'}
]

Here the solution posted before as a function: 这里将解决方案作为函数发布:

def filter1(dict_list, by='grenre'):
    seen   = []
    result = []
    for e in dict_list:
        if e[by] not in seen:
            seen.append(e[by])
            result.append(e)
    return result

And here a long-ish concatenation of lists: 这是一个长长的列表串联:

def filter2(dict_list, by='grenre'):
    result = [[e for e in dict_list if e[by]==key][0] for key in set([e[by] for e in dict_list])]
    return result

And a test: 并进行测试:

filter_key = 'genre'
print(filter1(data, by=filter_key))
print(filter2(data, by=filter_key))

暂无
暂无

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

相关问题 Python:从列表中删除具有相同键值的字典,以便该键的值对于列表中的所有字典都是唯一的 - Python: remove dictionaries from a list which have same value for a key so that the values of that key are unique for all the dictionaries in the list Python 2.7:根据2个词典列表中的一个键值查找公共元素 - Python 2.7 : Finding common elements based on one key value from 2 list of dictionaries 从具有特定键值的字典列表中删除元素 - Deleting elements from a list of dictionaries with certain key value 删除或更改列表的特定重复元素(并非所有重复元素) - Remove or change SPECIFIC repeated elements of a list (not all repeated elements) 将一个键值从词典列表转换为列表 - Convert One Key Value from List of Dictionaries to List 如果与列表中每个字典中的一个键的值匹配,则将具有不同词典值的新键添加到词典列表中 - Add new key to list of dictionaries with the values from a different dictionaries if matched with value of one key in each dictionary in a list 转置字典(从字典列表中提取一个键的所有值) - transpose dictionary (extract all the values for one key from a list of dictionaries) 在字典列表中按值打印 Python 关键元素 - Printing Python Key elements by Value in List of Dictionaries 如何根据所有词典中都不存在的特定键值从词典列表中获取值? - How to get value from list of dictionaries based on specific key value which does not exist in all dictionaries? 将一个字典列表的键和值添加到另一个字典列表 - Add key and value of one list of dictionaries to another list of dictionaries
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM