首页 > 解决方案 > #python 从列表中提取信息

问题描述

不是Python专家...我想从列表中提取信息。这是列表: 在此处输入图像描述

这是我的代码:

    for item in data ["items"]:
        for p in item ["standings"]["clan"]["participants"]:
                print("%s %s" % (
                    p["tag"],
                    p["name"],
))

出现此错误:

  File "_players_.py", line 26, in <module>
    for p in item ["standings"]["clan"]["participants"]:
TypeError: list indices must be integers or slices, not str

请问有什么帮助吗?

图片的另一个更大的视图: 在此处输入图像描述

谢谢

标签: pythonlist

解决方案


查看您的图片,会item["standings"]产生一个列表,而不是字典。这将通过错误来确认,该错误详细说明了使用字符串对列表进行索引。所以,我假设你会想要一些类似的东西:

...
for s in item["standings"]:
    for p in s["clan"]["participants"]:
        ...

推荐阅读