Guest User

Untitled

a guest
May 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. [{"id":"1","name":"Paul","age":"21"},
  2. {"id":"2","name":"Peter","age":"22"},
  3. {"id":"3","name":"Paul","age":"23"}]
  4.  
  5. [{"id":"1"."name":"Paul","age":"21"},
  6. {"id":"2","name":"Peter","age":"22"}]
  7.  
  8. import json
  9. ds = json.loads('python.json') #this file contains the json
  10. unique_stuff = { each['name'] : each for each in ds }.values()
  11.  
  12. all_ids = [ each['name'] for each in ds ]
  13. unique_stuff = [ ds[ all_ids.index(text) ] for text in set(texts) ]
  14.  
  15. print unique_stuff
  16.  
  17. import json
  18.  
  19. with open('python.json') as fp:
  20. ds = json.load(fp) #this file contains the json
  21.  
  22. mem = {}
  23.  
  24. for record in ds:
  25. name = record["name"]
  26. if name not in mem:
  27. mem[name] = record
  28.  
  29. print mem.values()
  30.  
  31. new = []
  32. seen = set()
  33. for record in old:
  34. name = record['name']
  35. if name not in seen:
  36. seen.add(name)
  37. new.append(record)
  38. del seen
  39.  
  40. ds = [{"id":"1","name":"Paul","age":"21"},
  41. {"id":"2","name":"Peter","age":"22"},
  42. {"id":"3","name":"Paul","age":"23"}]
  43.  
  44. {elem["name"]:elem for elem in ds}.values()
  45. Out[2]:
  46. [{'age': '23', 'id': '3', 'name': 'Paul'},
  47. {'age': '22', 'id': '2', 'name': 'Peter'}]
Add Comment
Please, Sign In to add comment