英文:
subtract two list of dict in python
问题
以下是翻译好的部分:
第一个列表
我的第一个列表看起来像这样:
[
{
"app_name": [
{
"date": "2023.01.04",
"platform": "web"
},
{
"date": "2022.12.13",
"platform": "web"
}
]
},
{
"another_app_name": [
{
"date": "2022.12.25",
"platform": "windows"
},
{
"date": "2022.12.31",
"platform": "ios"
}
]
}
]
第二个列表
我的第二个列表看起来像这样:
[
{
"app_name": [
{
"date": "2023.01.04",
"platform": "web"
}
]
},
{
"another_app_name": [
{
"date": "2022.12.31",
"platform": "ios"
}
]
}
]
结果应该如下所示:
[
{
"app_name": [
{
"date": "2022.12.13",
"platform": "web"
}
]
},
{
"another_app_name": [
{
"date": "2022.12.25",
"platform": "windows"
}
]
}
]
这是我的解决方案:
for f, b in zip(all_pcap, failed_pcap):
res = {key: f[key] - b.get(key, 0) for key in f}
英文:
I have two list like below and i want to subtract second one from first one, but i failed with solutions in the other questions.
First list
My first list is somthing look like below:
[
{
"app_name": [
{
"date": "2023.01.04",
"platform": "web"
},
{
"date": "2022.12.13",
"platform": "web"
}
]
},
{
"another_app_name": [
{
"date": "2022.12.25",
"platform": "windows"
},
{
"date": "2022.12.31",
"platform": "ios"
}
]
}
]
Second list
My Second list is somthing look like below:
[
{
"app_name": [
{
"date": "2023.01.04",
"platform": "web"
}
]
},
{
"another_app_name": [
{
"date": "2022.12.31",
"platform": "ios"
}
]
}
]
Resulte must be like:
[
{
"app_name": [
{
"date": "2022.12.13",
"platform": "web"
}
]
},
{
"another_app_name": [
{
"date": "2022.12.25",
"platform": "windows"
}
]
}
]
there is my solution:
for f, b in zip(all_pcap, failed_pcap):
res = {key: f[key] - b.get(key, 0) for key in f}
答案1
得分: 0
如果您的列表中的字典始终只包含单个{键: 值}
对,那么更好的数据结构可能是这样的:
apps = {
"app_name": [{"date": "2023.01.04", "platform": "web"}, {"date": "2022.12.13", "platform": "web"}],
"another_app_name": [{"date": "2022.12.25", "platform": "windows"}, {"date": "2022.12.31", "platform": "ios"}],
}
假设是这种情况,并且每个字典中的应用程序名称是唯一的,那么问题就会变得简单一些。
使用您提供的值:
a = [
{"app_name": [{"date": "2023.01.04", "platform": "web"}, {"date": "2022.12.13", "platform": "web"}]},
{"another_app_name": [{"date": "2022.12.25", "platform": "windows"}, {"date": "2022.12.31", "platform": "ios"}]}
]
b = [
{"app_name": [{"date": "2023.01.04", "platform": "web"}]},
{"another_app_name": [{"date": "2022.12.31", "platform": "ios"}]}
]
expected = [
{"app_name": [{"date": "2022.12.13", "platform": "web"}]},
{"another_app_name": [{"date": "2022.12.25", "platform": "windows"}]}
]
我们可以定义一个函数,将数据转换为更容易处理的格式,执行移除操作,然后将其转换回当前预期的格式:
def remove_app_entries(a, b):
a_transformed = {k: v for row in a for k, v in row.items()}
b_transformed = {k: v for row in b for k, v in row.items()}
results = {}
for app, data in a_transformed.items():
to_remove = b_transformed.get(app)
if not to_remove:
results[app] = data
else:
results[app] = [row for row in data if row not in to_remove]
return [{app: data} for app, data in results.items()]
这将生成与指定的预期数据相匹配的数据:
>>> remove_app_entries(a, b) == expected
True
如果数据已经按照我建议的格式,则可以跳过a_transformed
/ b_transformed
步骤,直接使用a
和b
。如果对返回值也适用,那么results
可以在最后简单地返回,而无需将其转换为列表。
英文:
If the dictionaries in your lists only ever contain a single {key: value}
pair, then a better data structure for the data would be something like this:
apps = {
"app_name": [{"date": "2023.01.04", "platform": "web"}, {"date": "2022.12.13", "platform": "web"}],
"another_app_name": [{"date": "2022.12.25", "platform": "windows"}, {"date": "2022.12.31", "platform": "ios"}],
}
Assuming that that is the case, and the app names are unique in each dictionary, then it makes the problem a bit easier.
Using the values you provided:
a = [
{"app_name": [{"date": "2023.01.04", "platform": "web"}, {"date": "2022.12.13", "platform": "web"}]},
{"another_app_name": [{"date": "2022.12.25", "platform": "windows"}, {"date": "2022.12.31", "platform": "ios"}]}
]
b = [
{"app_name": [{"date": "2023.01.04", "platform": "web"}]},
{"another_app_name": [{"date": "2022.12.31", "platform": "ios"}]}
]
expected = [
{"app_name": [{"date": "2022.12.13", "platform": "web"}]},
{"another_app_name": [{"date": "2022.12.25", "platform": "windows"}]}
]
We can define a function that transforms the data into a format that is easier to work with, perform the removals, and then transform it back into the currently expected format:
def remove_app_entries(a, b):
a_transformed = {k: v for row in a for k, v in row.items()}
b_transformed = {k: v for row in b for k, v in row.items()}
results = {}
for app, data in a_transformed.items():
to_remove = b_transformed.get(app)
if not to_remove:
results[app] = data
else:
results[app] = [row for row in data if row not in to_remove]
return [{app: data} for app, data in results.items()]
This results in data that matches the specified expected data:
>>> remove_app_entries(a, b) == expected
True
If the data was already in the format I suggested, then you would skip the a_transformed
/ b_transformed
steps and use a
and b
directly. If it also works for the return value, then results
could be simply returned at the end instead of transforming it into a list.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论