【问题标题】:Remove a dictinoary if empty list is found in Python如果在 Python 中找到空列表,则删除字典
【发布时间】:2020-11-01 14:42:13
【问题描述】:

我有一个字典列表。以下是我的示例数据:

[
  {
    "Customer_id": "123",
    "Account_no": "123456789012",
    "Amount": 3000,
    "Transaction": [
      {
        "date": "20/06/25",
        "time": "12:19:39",
        "Shop": "Amazon",
        "Comments": "Valuable items"
      }
    ]
  },
  {
    "Customer_id": "123",
    "Account_no": "123456789012",
    "Amount": 3000,
    "Transaction": []
  }
]

如果我发现事务列表为空,我需要删除字典。下面是我尝试的代码:

 res=list(filter(None,({key:val for key,val in sub.items() if val} for sub in results)))
 #results is the list of dictionary

我的输出:

[
  {
    "Customer_id": "123",
    "Account_no": "123456789012",
    "Amount": 3000,
    "Transaction": [
      {
        "date": "20/06/25",
        "time": "12:19:39",
        "Shop": "Amazon",
        "Comments": "Valuable items"
      }
    ]
  },
  {
    "Customer_id": "123",
    "Account_no": "123456789012",
    "Amount": 3000,
  }
]

我只能删除该特定列表,而不能删除与之相关的详细信息。

预期输出:

[
  {
    "Customer_id": "123",
    "Account_no": "123456789012",
    "Amount": 3000,
    "Transaction": [
      {
        "date": "20/06/25",
        "time": "12:19:39",
        "Shop": "Amazon",
        "Comments": "Valuable items"
      }
    ]
  }
]

请告诉我该怎么做。提前致谢!

【问题讨论】:

  • 使用列表理解,[v for v in results if v.get('Transaction')]

标签: python json-normalize


【解决方案1】:

您可以使用以下列表推导

res = [sub for sub in results if sub['Transaction']]

结果

[
    {
       'Customer_id': '123', 
       'Amount': 3000, 
       'Account_no': 
       '123456789012', 
       'Transaction': 
           [
               {
                   'Comments': 'Valuable items', 
                   'time': '12:19:39', 
                   'date': '20/06/25', 
                   'Shop': 'Amazon'
               }
          ]
     }
]

【讨论】:

  • 如果Transaction 键不存在怎么办?
【解决方案2】:
  • 尚不清楚数据的最终目标是什么,但如果您还想进行任何分析,则可以选择使用 pandas
  • 具体来说,使用pandas.json_normalize,它非常适合读取 JSON (dict) 数据列表
    • Transaction为空列表时,解析时将忽略该组数据
  • 给出以下示例,使用您的样本JSON 分配给data
  • 请注意数据框不包含数据,其中Transaction 为空
  • 现在可以分析数据了。
  • 根据示例数据,此答案假定 Transaction 键将始终存在。
import pandas as pd

# create the dataframe
df = pd.json_normalize(data, 'Transaction', ['Customer_id', 'Account_no', 'Amount'])

# display(df)
       date      time    Shop        Comments Customer_id    Account_no Amount
0  20/06/25  12:19:39  Amazon  Valuable items         123  123456789012   3000

【讨论】:

  • 感谢特伦顿!是的,我也可以为此使用熊猫。感谢您的解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-14
  • 2017-08-17
  • 1970-01-01
  • 2010-12-13
  • 2021-12-28
  • 2017-07-04
  • 1970-01-01
相关资源
最近更新 更多