我手頭有一份這樣的清單。在此串列中,我想通過從另一個串列中洗掉相同的串列來過濾deposits每個串列。witdrawal此集群當前集群超過 2 withdrawals,但這可能會有所不同。withdrawal因此,在一個回圈中盡可能多的集群, depositin onewithdrawal不應該在另一個withdrawal集群中。為此,我嘗試了各種 lambda 函式deposit id,但無法獲得所需的輸出。我怎樣才能提供這個?
exampleList = [
{
"withdrawal": {
"amount": 250,
"id": 70916631583,
"date": "31-05-22 - 16:14:08",
"paytype": "withdrawal"
},
"deposit": [
{
"id": 71018974368,
"amount": 120,
"date": "01-06-22 - 14:27:26",
"paytype": "deposit"
},
{
"id": 71018971332,
"amount": 100,
"date": "01-06-22 - 14:27:23",
"paytype": "deposit"
}
]
},
{
"withdrawal": {
"amount": 220,
"id": 71019072820,
"date": "01-06-22 - 14:28:40",
"paytype": "withdrawal"
},
"deposit": [
{
"id": 71033338591,
"amount": 100,
"date": "01-06-22 - 17:03:19",
"paytype": "deposit"
},
{
"id": 71033144597,
"amount": 250,
"date": "01-06-22 - 17:01:20",
"paytype": "deposit"
},
{
"id": 71018974368,
"amount": 120,
"date": "01-06-22 - 14:27:26",
"paytype": "deposit"
},
{
"id": 71018971332,
"amount": 100,
"date": "01-06-22 - 14:27:23",
"paytype": "deposit"
}
]
}
]
示例輸出:
exampleOutputList = [
{
"withdrawal": {
"amount": 250,
"id": 70916631583,
"date": "31-05-22 - 16:14:08",
"paytype": "withdrawal"
},
"deposit": [
{
"id": 71018974368,
"amount": 120,
"date": "01-06-22 - 14:27:26",
"paytype": "deposit"
},
{
"id": 71018971332,
"amount": 100,
"date": "01-06-22 - 14:27:23",
"paytype": "deposit"
}
]
},
{
"withdrawal": {
"amount": 220,
"id": 71019072820,
"date": "01-06-22 - 14:28:40",
"paytype": "withdrawal"
},
"deposit": [
{
"id": 71033338591,
"amount": 100,
"date": "01-06-22 - 17:03:19",
"paytype": "deposit"
},
{
"id": 71033144597,
"amount": 250,
"date": "01-06-22 - 17:01:20",
"paytype": "deposit"
}
]
}
]
我在示例列印輸出中顯示的帶有 id 的存款71018974368在下71018971332一個中不可用,因為它們在上一個提款集群中。這正是我想做的。這個撤消聚類可以超過2個,所以它可以變化,所以通過索引元素來做這個并不能解決我的問題。
我嘗試過這樣的事情。我等待它將 id 重新發送到一個空串列中并通過回圈過濾,但我得到的輸出沒有改變。
listLen = len(exampleList)
testList = []
if(listLen > 0):
while listLen > 0:
listLen -= 1
deposits = exampleList[listLen]['deposit']
withDrawal = exampleList[listLen]['withdrawal']
idList = [x['id'] for x in deposits]
filterFromList = list(filter(lambda x:x['id'] not in testList, deposits))
testList.append({"withdrawal" : withDrawal,"deposit" : filterFromList})
print(testList)
輸出
[{'withdrawal': {'amount': 220, 'id': 71019072820, 'date': '01-06-22 - 14:28:40', 'paytype': 'withdrawal'}, 'deposit': [{'id': 71033338591, 'amount': 100, 'date': '01-06-22 - 17:03:19', 'paytype': 'deposit'}, {'id': 71033144597, 'amount': 250, 'date': '01-06-22 - 17:01:20', 'paytype': 'deposit'}, {'id': 71018974368, 'amount': 120, 'date': '01-06-22 - 14:27:26', 'paytype': 'deposit'}, {'id': 71018971332, 'amount': 100, 'date': '01-06-22 - 14:27:23', 'paytype': 'deposit'}]}, {'withdrawal': {'amount': 250, 'id': 70916631583, 'date': '31-05-22 - 16:14:08', 'paytype': 'withdrawal'}, 'deposit': [{'id': 71018974368, 'amount': 120, 'date': '01-06-22 - 14:27:26', 'paytype': 'deposit'}, {'id': 71018971332, 'amount': 100, 'date': '01-06-22 - 14:27:23', 'paytype': 'deposit'}]}]
如輸出中所示,存在重復的存款 ID 和元素。
uj5u.com熱心網友回復:
List = [
{
"withdrawal": {
"amount": 250,
"id": 70916631583,
"date": "31-05-22 - 16:14:08",
"paytype": "withdrawal"
},
"deposit": [
{
"id": 71018974368,
"amount": 120,
"date": "01-06-22 - 14:27:26",
"paytype": "deposit"
},
{
"id": 71018971332,
"amount": 100,
"date": "01-06-22 - 14:27:23",
"paytype": "deposit"
}
]
},
{
"withdrawal": {
"amount": 220,
"id": 71019072820,
"date": "01-06-22 - 14:28:40",
"paytype": "withdrawal"
},
"deposit": [
{
"id": 71033338591,
"amount": 100,
"date": "01-06-22 - 17:03:19",
"paytype": "deposit"
},
{
"id": 71033144597,
"amount": 250,
"date": "01-06-22 - 17:01:20",
"paytype": "deposit"
},
{
"id": 71018974368,
"amount": 120,
"date": "01-06-22 - 14:27:26",
"paytype": "deposit"
},
{
"id": 71018971332,
"amount": 100,
"date": "01-06-22 - 14:27:23",
"paytype": "deposit"
}
]
}
]
def func(d):
if type(d)==list:
for i in reversed(range(len(d))):
v=d[i]
if v.get('id') in (71018974368,
71018971332):
d.pop(i)
else:
func(v)
elif type(d)==dict:
for k,v in d.items():
func(v)
func(List)
print(List)
uj5u.com熱心網友回復:
在遍歷資料時,您可以保留一組已經見過的 id。對于每個集群,保留一個未看到的 id 的側面串列,并在進入下一個集群之前替換“存款”串列。這比嘗試跟蹤嵌套集合的索引要容易得多。
seen = set()
for cluster in exampleList:
filtered = []
for deposit in cluster["deposit"]:
if deposit["id"] not in seen:
seen.add(deposit["id"])
filtered.append(deposit)
cluster["deposit"][:] = filtered
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/515897.html
