how to get modulus difference of values of two json objects in python
Question:
I have two JSON arrays , I need to get the modulus difference of the JSON object keys. My array list can have 1000s of elements. How to calculate it efficiently? Is there a way to do it parallelly without using loop?
For example
js1 = [{'myVal':100},{'myVal':200}]
js2 = [{'myVal':500},{'myVal':800}]
Result should be :
[{'myVal':400},{'myVal':600}]
Answers:
There is 2 way to acheive it :
- Using
map
js1 = [{'myVal':100},{'myVal':200}]
js2 = [{'myVal':500},{'myVal':800}]
result = list(map(lambda x, y: {'myVal': y['myVal'] - x['myVal']}, js1, js2))
print(result)
- Using list comprehension
js1 = [{'myVal':100},{'myVal':200}]
js2 = [{'myVal':500},{'myVal':800}]
result = [{'myVal': y['myVal'] - x['myVal']} for x,y in zip(js1,js2)]
print(result)
They will both output
[{'myVal': 400}, {'myVal': 600}]
I have two JSON arrays , I need to get the modulus difference of the JSON object keys. My array list can have 1000s of elements. How to calculate it efficiently? Is there a way to do it parallelly without using loop?
For example
js1 = [{'myVal':100},{'myVal':200}]
js2 = [{'myVal':500},{'myVal':800}]
Result should be :
[{'myVal':400},{'myVal':600}]
There is 2 way to acheive it :
- Using
map
js1 = [{'myVal':100},{'myVal':200}]
js2 = [{'myVal':500},{'myVal':800}]
result = list(map(lambda x, y: {'myVal': y['myVal'] - x['myVal']}, js1, js2))
print(result)
- Using list comprehension
js1 = [{'myVal':100},{'myVal':200}]
js2 = [{'myVal':500},{'myVal':800}]
result = [{'myVal': y['myVal'] - x['myVal']} for x,y in zip(js1,js2)]
print(result)
They will both output
[{'myVal': 400}, {'myVal': 600}]