繁体   English   中英

Python从字典列表列表中创建列表

[英]Python create list from a list of lists of dictionaries

我有一个list ,其中包含有关ZIP的信息(它们的位置,大小ect)的信息,该列表称为enclosures

该列表使用以下代码创建:

for item in g.entries:
    enclosure = [l for l in item["links"] if l["rel"] == "enclosure"]
    if(len(enclosure)>0):
        enclosures.append(enclosure)

列表enclosures中的每一项都有以下格式:

>>> enclosures[0]
[{'type': 'application/zip', 'rel': 'enclosure', 'length': '57648', 'href': 'http://www.sec.gov/Archives/edgar/data/37748/000003774810000025/0000037748-10-000025-xbrl.zip'}]

或另一个例子

>>> enclosures[45]
[{'type': 'application/zip', 'rel': 'enclosure', 'length': '107907', 'href': 'http://www.sec.gov/Archives/edgar/data/1385157/000104746910004400/0001047469-10-004400-xbrl.zip'}]

我需要创建一个名为hrefenclosures list ,该 enclosures list 以相同的顺序包含enclosures list 中的每个href项目。

下面的这些尝试均失败。

>>> enclosures[46]["href"]
Traceback (most recent call last):
  File "<pyshell#66>", line 1, in <module>
    enclosures[46]["href"]
TypeError: list indices must be integers, not str

>>> enclosures[46][4]
Traceback (most recent call last):
  File "<pyshell#67>", line 1, in <module>
    enclosures[46][4]
IndexError: list index out of range

编辑

亲爱的timgeb

我有这个结果:

>>> href = [x['href'] for x in enclosures]
Traceback (most recent call last):
  File "<pyshell#75>", line 1, in <module>
    href = [x['href'] for x in enclosures]
  File "<pyshell#75>", line 1, in <listcomp>
    href = [x['href'] for x in enclosures]
TypeError: list indices must be integers, not str
href = [x[0]['href'] for x in enclosures]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM