简体   繁体   English

根据另一个字典列表中的键值删除字典

[英]Delete a dictionary based on the value of a key in another list of dictionaries

I have a list of dictionaries and a main dictionary. 我有一个字典清单和一本主要字典。 List of dictionaries have the following format. 词典列表具有以下格式。 Values are assigned a variable that changes dynamically in the program. 为值分配一个在程序中动态变化的变量。

list_dict = [{'url': url_value , 'title' : title_value}, {'url': url_value , 'title' : title_value}]

main_dict = {"execution_time": "2017-06-05", "target_url": "http://www.bloomberg.com", "data": [{ "url" : url1}, { "url" : url2}], "name": "Michael", "occupation": "software"}

If any url value(url1 or url2) under data in main_dict is the same value as the url_value in any of the dictionaries in list_dict, I want to delete that dictionary from the data. 如果main_dict中数据下的任何url值(url1或url2)与list_dict中任何词典中的url_value相同,我想从数据中删除该词典。

Output: Assuming url_value is url1 then: 输出:假设url_value为url1,则:

main_dict = {"execution_time": "2017-06-05", "target_url": "http://www.bloomberg.com", "data": [{ "url" : url2}], "name": "Michael", "occupation": "software"}

I thought about using dict comprehensions, however everything I tried did not work. 我考虑过使用dict理解,但是我尝试的所有方法都没有用。 I would appreciate a starting point or any guidance. 我将很高兴有一个起点或任何指导。

This should do the trick: 这应该可以解决问题:

my_list = [list_dict_data['url'] for list_dict_data in list_dict]
delete_list = []

for i in range(len(main_dict['data'])):
  if main_dict['data'][i]['url'] in my_list:
    delete_list.append(i)

for i in delete_list:
  del main_dict['data'][i]

So what it does is: 所以它的作用是:

  1. List all the urls to check from list_dict 列出所有要从list_dict检查的URL
  2. Iterate through all the urls in main dict and compare it with the data in list_dict 遍历主字典中的所有URL,并将其与list_dict中的数据进行比较
  3. Add the index to the delete list 将索引添加到删除列表
  4. Iterate through the delete list and delete the corresponding index in main_dict 遍历删除列表并删除main_dict中的相应索引

Try optimising this though as its really crude. 尝试对其进行优化,因为它确实很粗糙。

You can try this: 您可以尝试以下方法:

>>> list_dict = [{'url': "url1" , 'title' : "title_value1"}, {'url': "other_url" , 'title' : "title_value2"}]
>>> main_dict = {"execution_time": "2017-06-05", "target_url": "http://www.bloomberg.com", "data": [{ "url" : "url1"}, { "url" : "url2"}], "name": "Michael", "occupation": "software"}
>>> S = set(d["url"] for d in list_dict)
>>> main_dict["data"] = [d for d in main_dict["data"] if d["url"] not in S]
>>> main_dict
{'execution_time': '2017-06-05', 'target_url': 'http://www.bloomberg.com', 'data': [{'url': 'url2'}], 'name': 'Michael', 'occupation': 'software'}

Instead of deleting elements of main_dict["data"] , the idea is to recreate the list without the matchings urls: 代替删除main_dict["data"]元素,想法是重新创建没有匹配URL的列表:

  • extract the distinct urls of the list_dict in S ; 提取S list_dict的不同网址;
  • filter the dicts d in main_dict["data"] on the rule: d["url"] not in S . 根据规则过滤main_dict["data"]的字典dd["url"] not in S

Note on naming: try to name your variables according to the content and not the type . 命名注意:请尝试根据内容而不是类型来命名变量。

  • list_dict is a list of dictionaries (I can see it), but I would like to know immediately what's in those dictionaries. list_dict是词典列表(我可以看到),但是我想立即知道这些词典中的内容。 web_pages would be better, if you accept that an url + a title makes a page. 如果您接受url +标题构成页面,则web_pages会更好。 But you should specify why those pages are on this list (eg dead_link_pages , or whatever) 但是您应该指定为什么这些页面在此列表中(例如dead_link_pages或其他)
  • main_dict is a dictionary (pretty obvious and not really informative): something like task is better. main_dict是一本字典(很明显,但不是很main_dict ):像task这样的东西更好。 Again, a better specification is informative: update_task , retrieve_task , ? 同样,更好的规范是update_taskupdate_taskretrieve_task update_task
  • ok, I replace S by page_urls ! 好的,我用page_urls代替S

Have a look, this is far more readable: 看看,这更具可读性:

>>> web_pages = [{'url': "url1" , 'title' : "title_value1"}, {'url': "other_url" , 'title' : "title_value2"}]
>>> task = {"execution_time": "2017-06-05", "target_url": "http://www.bloomberg.com", "data": [{ "url" : "url1"}, { "url" : "url2"}], "name": "Michael", "occupation": "software"}
>>> page_urls = set(p["url"] for p in web_pages)
>>> task["data"] = [t for t in task["data"] if t["url"] not in page_urls]
>>> task
{'execution_time': '2017-06-05', 'target_url': 'http://www.bloomberg.com', 'data': [{'url': 'url2'}], 'name': 'Michael', 'occupation': 'software'}

暂无
暂无

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

相关问题 如何根据另一个字典替换列表字典中键的值? - How to replace a value for a key in dictionaries of a list based on another dictionary? 根据基于该字典中另一个键的值的条件,更新python词典列表中的值 - Update a value in a list of dictionaries in python based on a condition based on the value of another key in that dictionary 如何根据用户指定的值从字典列表中的字典中删除键? - How do you delete a key from a dictionary within a list of dictionaries based on a user specified value? 根据字典列表中的特定字典键检测和删除重复项 - Detect and delete duplicates based on specific dictionary key in a list of dictionaries 有没有办法根据一个字典中的值小于另一个字典中的相同键来过滤字典列表? - Is there a way to filter a list of dictionaries based on a value in one dictionary being less than the same key in another? 如何根据字典列表中的另一个值有效地查找字典值 - How to efficiently find a dictionary value based on another value in a list of dictionaries 根据字典列表中的另一个值(键的)检索键的值 - Retrieve a value of a key based on another value(of key) in the list of dictionaries 根据该字典中某个键的特定值过滤字典列表 - filter list of dictionaries based on a particular value of a key in that dictionary 根据键值对更新字典列表中的整个字典 - update an entire dictionary in a list of dictionaries based on key value pair 根据另外两个字典替换一个字典值列表 - Replace a dictionary value list based on another two dictionaries
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM