从列表中获取列表

Car*_*lho -1 python dictionary list

我有此列表,其内容如下:

quake_list = [{'time': '2016-01-12T21:05:59.000Z',
               'location': {'latitude': 60.5079, 'longitude': -142.9635},
               'event': {'magnitude': 1.3, 'depth': 9.1},
               'type': 'earthquake',
               'status': 'reviewed'},
              {'time': '2016-01-12T21:02:24.760Z',
               'location': {'latitude': 38.7978325, 'longitude': -122.7196655},
               'event': {'magnitude': 1.0, 'depth': -0.77},
               'type': 'earthquake',
               'status': 'automatic'},
               ...]
Run Code Online (Sandbox Code Playgroud)

另一件事,我需要过滤掉None条目看来,文件没有该值的条目。

这就是为什么我收到错误消息

TypeError:列表索引必须是整数或切片,而不是str

我需要为此创建一个列表,其中包含所有值:

magnitude = [1.3,1.0]

谢谢您的帮助。

CDJ*_*DJB 6

您可以执行以下操作:

>>> [d['event']['magnitude'] for d in quake_list]
[1.3, 1.0]
Run Code Online (Sandbox Code Playgroud)

要仅在地震记录时记录震级,请使用:

>>> [d['event']['magnitude'] for d in quake_list if d['type'] == 'earthquake']
Run Code Online (Sandbox Code Playgroud)