【问题标题】:My discord bot is not looping through the entire entire array it needs to我的不和谐机器人没有循环遍历它需要的整个数组
【发布时间】:2020-10-09 05:30:54
【问题描述】:

我目前有一个脚本,它遍历 JSON 结果列表并为每个结果提取某些信息。我目前正在尝试为位于 animalURLS 列表中的每个结果提取标签。

如果我不添加if statement,循环将成功通过列表。但是,如果我要添加if statement,输出会发生变化,它只会遍历我拥有的整个结果列表的一部分。使用if statement,它将打印的最大项目数为 85。即使有适当的标签,也会有某些结果被遗漏。

以下是可能的标签列表:

  • 其他

我不确定为什么我的代码无法获得某些结果,希望能提供任何帮助。

            for i, v in enumerate(miskaJSON[str(ctx.guild.id)]["animalURLS"]):
                if v[2] == tag:
                    print(v)
                    print(i)

这是 miskaJSONStructure 的一个示例,这是我的代码所基于的。标记是 animalURLS 列表中的最后一项:

    "628704494615789599": {
        "prefix": "%",
        "animalURLS": [
            [
                "Darth Kylo-Kun",
                "https://cdn.discordapp.com/attachments/628704495047671818/723325532397109329/IMG_20170531_140251.png",
                "cat"
            ]
        ],
        "tags": [
            "cat",
            "dog",
            "bird",
            "other"
        ]
    }

【问题讨论】:

  • 你能把miskaJSON结构放上去吗
  • 请稍等
  • 添加 json 结构
  • 控制台中显示的任何错误?

标签: python python-3.x list discord.py


【解决方案1】:

请注意,这个答案是基于我的假设。如果没有看到实际数据,我将无法确认。

根据我所看到的,animalURLS 列表包含三项内容:

  • 发帖人的评论
  • 图片网址
  • 标签

您的答案有效的原因是,animalURLS 中可能有一些额外的项目,其中包含一些其他信息。这意味着列表中的项目将超过三个,v[2] 可能并不总是一个标签。

for i, v in enumerate(miskaJSON[str(ctx.guild.id)]["animalURLS"]):
    # v[-1] would get the last item on the list which you are assuming would be a tag.
    if v[-1] == tag:
        validIndices.append(i)

如果不是标签,要查看v[2]是什么,可以尝试以下方法:

valid_tags = []
invalid_tags = []

for i, v in enumerate(miskaJSON[str(ctx.guild.id)]["animalURLS"]):
    if v[2] == tag:
        valid_tags.append(v[2])
    else:
        invalid_tags.append(v[2])

print(f"Total Valid Tags: {len(valid_tags)} | Valid Tags: {valid_tags}")
print(f"Total Invalid Tags: {len(invalid_tags)} | Invalid Tags: {invalid_tags}")

我会这样做,假设 animalURLS 中的最后一项始终是标签,如果这是您的目标,您可以使用以下内容检查标签是否与您的标签列表匹配。

for i, v in enumerate(miskaJSON[str(ctx.guild.id)]["animalURLS"]):
    if v[-1] in miskaJSON[str(ctx.guild.id)]["tags"]:
        print(v)
        print(i)

预期的输入 JSON:

    "628704494615789599": {
        "prefix": "%",
        "animalURLS": [
            [
                "Darth Kylo-Kun", # Comment
                "https://cdn.discordapp.com/attachments/628704495047671818/723325532397109329/IMG_20170531_140251.png", # URL
                "cat" # Tag
            ]
        ],
        "tags": [
            "cat",
            "dog",
            "bird",
            "other"
        ]
    }

可能的意外输入 JSON:

    "628704494615789599": {
        "prefix": "%",
        "animalURLS": [
            [
                "Darth Kylo-Kun", # Comment
                "random stuff in between",
                "https://cdn.discordapp.com/attachments/628704495047671818/723325532397109329/IMG_20170531_140251.png", # URL
                "random stuff in between",
                "cat" # Tag
            ]
        ],
        "tags": [
            "cat",
            "dog",
            "bird",
            "other"
        ]
    }

【讨论】:

    【解决方案2】:
    for i, v in enumerate(miskaJSON[str(ctx.guild.id)]["animalURLS"]):
        if v[-1] == tag:
            validIndices.append(i)
    

    不知何故,这已解决,我不知道为什么

    【讨论】:

    • 可能是“animalURLS”有超过 3 个值。这就是它起作用的原因
    猜你喜欢
    • 2017-05-14
    • 2011-05-12
    • 2018-11-29
    • 2021-11-18
    • 2021-09-30
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多