[英]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: 所以它的作用是:
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的列表:
list_dict
in S
; S
list_dict
的不同网址; d
in main_dict["data"]
on the rule: d["url"] not in S
. main_dict["data"]
的字典d
: d["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. 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_task
: update_task
, retrieve_task
update_task
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.