[英]Remove all elements of a string list in python if they contain a given phrase
Have a list called summary
containing a corresponding JSON object similar to:有一个名为
summary
的列表,其中包含类似于以下内容的相应 JSON 对象:
{
# summary[0]
"object1": {
"json_data": "{json data information}",
"tables_data": "TABLES_DATA"
},
# summary[1]
"object2": {
"json_data": "{json data information}",
"tables_data": ""
}
}
Essentially, if "tables_data": ""
is found within the string of a list item, I want the entire list item to be removed.本质上,如果在列表项的字符串中找到
"tables_data": ""
,我希望删除整个列表项。
How would I go about doing this?我该怎么做呢?
You can do a dictionary-comprehension selecting the dictionary item with 'tables_data'
has a value not equals ''
:您可以进行字典理解,选择带有
'tables_data'
的值不等于''
的字典项:
summary = [{
# summary[0]
"object1": {
"json_data": "{json data information}",
"tables_data": "TABLES_DATA"
},
# summary[1]
"object2": {
"json_data": "{json data information}",
"tables_data": ""
}
}]
result = [{k: d for k, d in s.items() if d.get('tables_data') != ''} for s in summary]
# [{'object1': {'json_data': '{json data information}', 'tables_data': 'TABLES_DATA'}}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.