在Python中减去两个字典列表

huangapple go评论96阅读模式
英文:

subtract two list of dict in python

问题

以下是翻译好的部分:

第一个列表

我的第一个列表看起来像这样:

  1. [
  2. {
  3. "app_name": [
  4. {
  5. "date": "2023.01.04",
  6. "platform": "web"
  7. },
  8. {
  9. "date": "2022.12.13",
  10. "platform": "web"
  11. }
  12. ]
  13. },
  14. {
  15. "another_app_name": [
  16. {
  17. "date": "2022.12.25",
  18. "platform": "windows"
  19. },
  20. {
  21. "date": "2022.12.31",
  22. "platform": "ios"
  23. }
  24. ]
  25. }
  26. ]

第二个列表

我的第二个列表看起来像这样:

  1. [
  2. {
  3. "app_name": [
  4. {
  5. "date": "2023.01.04",
  6. "platform": "web"
  7. }
  8. ]
  9. },
  10. {
  11. "another_app_name": [
  12. {
  13. "date": "2022.12.31",
  14. "platform": "ios"
  15. }
  16. ]
  17. }
  18. ]

结果应该如下所示:

  1. [
  2. {
  3. "app_name": [
  4. {
  5. "date": "2022.12.13",
  6. "platform": "web"
  7. }
  8. ]
  9. },
  10. {
  11. "another_app_name": [
  12. {
  13. "date": "2022.12.25",
  14. "platform": "windows"
  15. }
  16. ]
  17. }
  18. ]

这是我的解决方案:

  1. for f, b in zip(all_pcap, failed_pcap):
  2. 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:

  1. [
  2. {
  3. "app_name": [
  4. {
  5. "date": "2023.01.04",
  6. "platform": "web"
  7. },
  8. {
  9. "date": "2022.12.13",
  10. "platform": "web"
  11. }
  12. ]
  13. },
  14. {
  15. "another_app_name": [
  16. {
  17. "date": "2022.12.25",
  18. "platform": "windows"
  19. },
  20. {
  21. "date": "2022.12.31",
  22. "platform": "ios"
  23. }
  24. ]
  25. }
  26. ]

Second list

My Second list is somthing look like below:

  1. [
  2. {
  3. "app_name": [
  4. {
  5. "date": "2023.01.04",
  6. "platform": "web"
  7. }
  8. ]
  9. },
  10. {
  11. "another_app_name": [
  12. {
  13. "date": "2022.12.31",
  14. "platform": "ios"
  15. }
  16. ]
  17. }
  18. ]

Resulte must be like:

  1. [
  2. {
  3. "app_name": [
  4. {
  5. "date": "2022.12.13",
  6. "platform": "web"
  7. }
  8. ]
  9. },
  10. {
  11. "another_app_name": [
  12. {
  13. "date": "2022.12.25",
  14. "platform": "windows"
  15. }
  16. ]
  17. }
  18. ]

there is my solution:

  1. for f, b in zip(all_pcap, failed_pcap):
  2. res = {key: f[key] - b.get(key, 0) for key in f}

答案1

得分: 0

如果您的列表中的字典始终只包含单个{键: 值}对,那么更好的数据结构可能是这样的:

  1. apps = {
  2. "app_name": [{"date": "2023.01.04", "platform": "web"}, {"date": "2022.12.13", "platform": "web"}],
  3. "another_app_name": [{"date": "2022.12.25", "platform": "windows"}, {"date": "2022.12.31", "platform": "ios"}],
  4. }

假设是这种情况,并且每个字典中的应用程序名称是唯一的,那么问题就会变得简单一些。

使用您提供的值:

  1. a = [
  2. {"app_name": [{"date": "2023.01.04", "platform": "web"}, {"date": "2022.12.13", "platform": "web"}]},
  3. {"another_app_name": [{"date": "2022.12.25", "platform": "windows"}, {"date": "2022.12.31", "platform": "ios"}]}
  4. ]
  5. b = [
  6. {"app_name": [{"date": "2023.01.04", "platform": "web"}]},
  7. {"another_app_name": [{"date": "2022.12.31", "platform": "ios"}]}
  8. ]
  9. expected = [
  10. {"app_name": [{"date": "2022.12.13", "platform": "web"}]},
  11. {"another_app_name": [{"date": "2022.12.25", "platform": "windows"}]}
  12. ]

我们可以定义一个函数,将数据转换为更容易处理的格式,执行移除操作,然后将其转换回当前预期的格式:

  1. def remove_app_entries(a, b):
  2. a_transformed = {k: v for row in a for k, v in row.items()}
  3. b_transformed = {k: v for row in b for k, v in row.items()}
  4. results = {}
  5. for app, data in a_transformed.items():
  6. to_remove = b_transformed.get(app)
  7. if not to_remove:
  8. results[app] = data
  9. else:
  10. results[app] = [row for row in data if row not in to_remove]
  11. return [{app: data} for app, data in results.items()]

这将生成与指定的预期数据相匹配的数据:

  1. >>> remove_app_entries(a, b) == expected
  2. True

如果数据已经按照我建议的格式,则可以跳过a_transformed / b_transformed步骤,直接使用ab。如果对返回值也适用,那么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:

  1. apps = {
  2. "app_name": [{"date": "2023.01.04", "platform": "web"}, {"date": "2022.12.13", "platform": "web"}],
  3. "another_app_name": [{"date": "2022.12.25", "platform": "windows"}, {"date": "2022.12.31", "platform": "ios"}],
  4. }

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:

  1. a = [
  2. {"app_name": [{"date": "2023.01.04", "platform": "web"}, {"date": "2022.12.13", "platform": "web"}]},
  3. {"another_app_name": [{"date": "2022.12.25", "platform": "windows"}, {"date": "2022.12.31", "platform": "ios"}]}
  4. ]
  5. b = [
  6. {"app_name": [{"date": "2023.01.04", "platform": "web"}]},
  7. {"another_app_name": [{"date": "2022.12.31", "platform": "ios"}]}
  8. ]
  9. expected = [
  10. {"app_name": [{"date": "2022.12.13", "platform": "web"}]},
  11. {"another_app_name": [{"date": "2022.12.25", "platform": "windows"}]}
  12. ]

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:

  1. def remove_app_entries(a, b):
  2. a_transformed = {k: v for row in a for k, v in row.items()}
  3. b_transformed = {k: v for row in b for k, v in row.items()}
  4. results = {}
  5. for app, data in a_transformed.items():
  6. to_remove = b_transformed.get(app)
  7. if not to_remove:
  8. results[app] = data
  9. else:
  10. results[app] = [row for row in data if row not in to_remove]
  11. return [{app: data} for app, data in results.items()]

This results in data that matches the specified expected data:

  1. >>> remove_app_entries(a, b) == expected
  2. 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.

huangapple
  • 本文由 发表于 2023年1月9日 04:51:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定