2016-05-16 114 views
-2

需要您的幫助來尋找解決方案。我有以下jsonpython json匹配多個並替換

[{「source」:「AA」,「target」:「BB」,「metric」:「10」,「local_interface」:「100」,「remote_interface」:「200」 }, {「source」:「AA」,「target」:「BB」,「metric」:「10」,「local_interface」:「111」,「remote_interface」:「222」}, {「source」 : 「BB」, 「目標」: 「AA」, 「指標」: 「10」, 「local_interface」: 「200」, 「remote_interface」: 「100」}]

目標是:

  • 獲取作爲源/目標/本地接口的第一個元素(即:模式)
  • 搜索剩餘的元素東西匹配源= pattern.target,目標= pattern.source,remote_interface = pattern.local_interface
  • 從json中刪除元素。

希望這是有道理的。

+0

那麼你的代碼在哪裏,它的問題究竟是什麼? – jonrsharpe

回答

0

你是在追求這麼簡單的東西嗎?

j = [{"source":"AA","target":"BB","metric":"10", 
"local_interface":"100","remote_interface":"200"}, 
{"source":"AA","target":"BB","metric":"10", 
"local_interface":"111","remote_interface":"222"}, 
{"source":"BB","target":"AA","metric":"10", 
"local_interface":"200","remote_interface":"100"}] 

s = j[0]["source"] 
print(s) 
t = j[0]["target"] 
print(t) 
li = j[0]["local_interface"] 
print(li) 

print('Find source ==', t) 
for i in range(1,3): 
    if j[i]['source'] == t: 
    print("Is match:", j[i]) 

print() 

del(j[1]) 
print("after deletion:", j) 
+0

thx Jorgen。我需要檢查所有三個值。我猜如果j [i] ['source'] == t並且j [i] ['target'] == s和j [i] ['remote_interface'] == li:將會起作用 – Cmarv

+0

當然可以。而要刪除的數組元素是i中的索引 – Jorgen