比较JSON对象部分上的列表或集合

比较JSON对象部分上的列表或集合,json,python-2.7,set,Json,Python 2.7,Set,我的输入是这样的: inputData=[] inputData.append({"CustomerName": "CustomerA","State": "StateA","ItemNumber": "Item1"}) inputData.append({"CustomerName": "CustomerA","State": "StateA","ItemNumber": "Item2"}) inputData.append({"CustomerName": "CustomerB","Stat

我的输入是这样的:

inputData=[]
inputData.append({"CustomerName": "CustomerA","State": "StateA","ItemNumber": "Item1"})
inputData.append({"CustomerName": "CustomerA","State": "StateA","ItemNumber": "Item2"})
inputData.append({"CustomerName": "CustomerB","State": "StateB","ItemNumber": "Item1"})
inputData.append({"CustomerName": "CustomerB","State": "StateB","ItemNumber": "Item2"})
inputData.append({"CustomerName": "CustomerX","State": "StateX","ItemNumber": "Item1"})
inputData.append({"CustomerName": "CustomerX","State": "StateX","ItemNumber": "Item2"})
这是我比较的清单,以确定客户是否可以购买该商品

allowedCustomers = ["CustomberA","CustomberB"]
以下是我比较列表的方式:

unauthorizedCustomers = list(set(inputData)-set(allowedCustomers))
如何修改上述语句,以便仅在CustomerName上进行比较,而未经授权的Customers list拥有
CustomerX
的完整数据

[{"CustomerName": "CustomerX","State": "StateX","ItemNumber": "Item1"},
{"CustomerName": "CustomerX","State": "StateX","ItemNumber": "Item2"})]

您应该阅读列表理解,以了解这里发生了什么

以下是使用json和列表可以做的事情:

 import json
 inputData=[]
 inputData.append({"CustomerName": "CustomerA","State": "StateA","ItemNumber": "Item1"})
 inputData.append({"CustomerName": "CustomerA","State": "StateA","ItemNumber": "Item2"})
 inputData.append({"CustomerName": "CustomerB","State": "StateB","ItemNumber": "Item1"})
 inputData.append({"CustomerName": "CustomerB","State": "StateB","ItemNumber": "Item2"})
 inputData.append({"CustomerName": "CustomerX","State": "StateX","ItemNumber": "Item1"})
 inputData.append({"CustomerName": "CustomerX","State": "StateX","ItemNumber": "Item2"})

 allowedCustomers = ["CustomerA","CustomerB"]
 json_array = json.loads(json.dumps(inputData))
 # Now filter required customer based on specific property.
 allowed_customers = [customer for customer in json_array if customer['CustomerName'] in allowedCustomers]
 import json
 inputData=[]
 inputData.append({"CustomerName": "CustomerA","State": "StateA","ItemNumber": "Item1"})
 inputData.append({"CustomerName": "CustomerA","State": "StateA","ItemNumber": "Item2"})
 inputData.append({"CustomerName": "CustomerB","State": "StateB","ItemNumber": "Item1"})
 inputData.append({"CustomerName": "CustomerB","State": "StateB","ItemNumber": "Item2"})
 inputData.append({"CustomerName": "CustomerX","State": "StateX","ItemNumber": "Item1"})
 inputData.append({"CustomerName": "CustomerX","State": "StateX","ItemNumber": "Item2"})

 allowedCustomers = ["CustomerA","CustomerB"]
 json_array = json.loads(json.dumps(inputData))
 # Now filter required customer based on specific property.
 allowed_customers = [customer for customer in json_array if customer['CustomerName'] in allowedCustomers]