I have a list of dictionaries in python:
thedata = [{'date': '2002-02', 'data': 2.0},
{'date': '2002-03', 'data': 2.0017}...]
How do I make a list of just the 'data' values?:
[2.0, 2.0017...]
I've tried:
justFigures = list(filter(lambda x: x["data"], thedata))
You can try like so:
thedata = [{'date': '2002-02', 'data': 2.0},
{'date': '2002-03', 'data': 2.0017}]
print([a['data'] for a in thedata])
Output:
[2.0, 2.0017]
I would use a list comprehension
In [1]: thedata = [{'date': '2002-02', 'data': 2.0},
{'date': '2002-03', 'data': 2.0017}]
In [2]: just_figures = [ d['data'] for d in thedata ]
In [3]: just_figures
Out[3]: [2.0, 2.0017]
thedata = [{'date': '2002-02', 'data': 2.0},
{'date': '2002-03', 'data': 2.0017}]
# back to your own way, lambda
# py 2
print map(lambda a : a["data"], thedata)
# py 3
print (list(map(lambda a : a["data"], thedata)))
>>> [2.0, 2.0017]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.