我對python非常陌生。如果值重複,則從數組中刪除json項python
我有一個數組充滿json對象。一些json對象包含重複的值。該陣列是這樣的:
[{"id":"1"."name":"Paul","age":"21"},
{"id":"2","name":"Peter","age":"22"},
{"id":"3","name":"Paul","age":"23"}]
我所試圖做的是,如果name
是一樣的另一個JSON對象刪除一個項目,並留下數組中的第一個。
因此,在這種情況下,我應該留下
[{"id":"1"."name":"Paul","age":"21"},
{"id":"2","name":"Peter","age":"22"}]
我現在有可以看到下面,代碼可以在很大程度上based on this answer:
import json
ds = json.loads('python.json') #this file contains the json
unique_stuff = { each['name'] : each for each in ds }.values()
all_ids = [ each['name'] for each in ds ]
unique_stuff = [ ds[ all_ids.index(text) ] for text in set(texts) ]
print unique_stuff
我什至不知道這行當我嘗試ds = json.loads('python.json') #this file contains the json
和print ds
沒有顯示在控制檯中。
請告訴我們您的非工作代碼,以便我們可以幫你解決這個問題。 –
@ PM2Ring我更新了我的問題 –