我想从这种列表中摘录:
[{'time': '2018-10-08T13:26:40.366296729Z', 'ask0': 6604.036},
{'time': '2018-10-08T13:26:45.666245795Z', 'ask0': 6607.34909},
{'time': '2018-10-08T13:26:51.08045435Z', 'ask0': 6607.18674}]
是否可以在一行中创建asks0列表?我尝试过这样的事情:
asks = [y for x, y in points.__iter__()]
但是它不起作用。
答案 0 :(得分:4)
data = [
{"time": "2018-10-08T13:26:40.366296729Z", "ask0": 6604.036},
{"time": "2018-10-08T13:26:45.666245795Z", "ask0": 6607.34909},
{"time": "2018-10-08T13:26:51.08045435Z", "ask0": 6607.18674},
]
asks = [d["ask0"] for d in data]
# asks will be [6604.036, 6607.34909, 6607.18674]
答案 1 :(得分:3)
简单地:
points = [{'time': '2018-10-08T13:26:40.366296729Z', 'ask0': 6604.036},
{'time': '2018-10-08T13:26:45.666245795Z', 'ask0': 6607.34909},
{'time': '2018-10-08T13:26:51.08045435Z', 'ask0': 6607.18674}]
[i['ask0'] for i in points]
#[6604.036, 6607.34909, 6607.18674]