Reputation: 83
I'm having trouble with this. I have the following list:
listA = [
[
{u'source': u'manual', u'value': 10},
{u'source': u'manual', u'value': 10},
{u'source': u'manual', u'value': 20},
{u'source': u'manual', u'value': 30},
{u'source': u'manual', u'value': 10},
{u'source': u'manual', u'value': 10},
{u'source': u'manual', u'value': 10},
{u'source': u'manual', u'value': 30}
],
[
{u'source': u'manual', u'value': 20},
{u'source': u'manual', u'value': 50},
{u'source': u'manual', u'value': 80},
{u'source': u'manual', u'value': 60},
{u'source': u'manual', u'value': 10},
{u'source': u'manual', u'value': 10},
{u'source': u'manual', u'value': 40},
{u'source': u'manual', u'value': 30}
],
[
{u'source': u'manual', u'value': 60},
{u'source': u'manual', u'value': 20},
{u'source': u'manual', u'value': 40},
{u'source': u'manual', u'value': 30},
{u'source': u'manual', u'value': 20},
{u'source': u'manual', u'value': 10},
{u'source': u'manual', u'value': 50},
{u'source': u'manual', u'value': 10}
]
]
What I want to do is to loop the nested list, extract the dictionary value (first position of first nested list, first position of second nested list, fist position of third nested list --> second position of first nested list, second position of second nested list, second position of third nested list, etc...) and then find the median value of the 'x' position values.
Thank you!
Upvotes: 0
Views: 168
Reputation: 61910
If you want numpy you could use numpy.median:
import numpy as np
result = np.median([[i['value'] for i in l] for l in zip(*listA)], axis=1)
print(result)
Output
[20. 20. 40. 30. 10. 10. 40. 30.]
Upvotes: 1
Reputation: 88276
You could use a list comprehension with zip
:
from statistics import median
[median(i['value'] for i in l) for l in zip(*listA)]
# [20, 20, 40, 30, 10, 10, 40, 30]
Upvotes: 2