0

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

[
  {
    "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"
      }
    ]
  }
]

请让我知道该怎么做。提前致谢!

4

2 回答 2

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'
               }
          ]
     }
]

于 2020-07-12T07:35:00.877 回答
1
  • 目前尚不清楚数据的最终目标是什么,但如果您还想进行任何分析,则可以选择使用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
于 2020-07-12T07:54:46.073 回答