如何在di中的列表中逐个打印值

2024-04-25 23:12:20 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我一直在试图找出如何打印一个值一个接一个的列表,这是在一个dict内。我将张贴一个代码下面它看起来如何,以及评论。你知道吗

class Notifications():
    def __init__(self):

        self.discordFiltered = {'swedish': ['https://discordapp.com/api/webhooks/xxxxxxxxxxxxxxxxxx/zeASB62I35Fpssm',
                                          'https://discordapp.com/api/webhooks/xxxxxxxxxxxxxxxxxx/YdfzrT3-tp55MMfxq'],

                                'mixed': ['https://discordapp.com/api/webhooks/xxxxxxxxxxxxxxxxxx/v-a62eZdnwGO95kmZ',
                                        'https://discordapp.com/api/webhooks/xxxxxxxxxxxxxxxxxx/v9_oyW_IzuncYo6LEGS']
        }

        self.discordUnfiltered = {'swedish': ['https://discordapp.com/api/webhooks/xxxxxxxxxxxxxxxxxx/WjFoBneVWMaI9',
                                            'https://discordapp.com/api/webhooks/xxxxxxxxxxxxxxxxxx/yqG_aMU8dM4PSb'],

                                  'mixed': ['https://discordapp.com/api/webhooks/xxxxxxxxxxxxxxxxxx/F1iQ2T4ZUa2rOi',
                                           'https://discordapp.com/api/webhooks/xxxxxxxxxxxxxxxxxx/EGgb9XV95Nbu_bb']
        }


    def sendToDiscord(self, item, isTrueFalse, directory: Path = Path(r"./slack")):
        for filepath in directory.glob("*.json"): #Will loop only twice because there is only 2 json files. Thrill.json and HelloWorld.json
            with open(filepath.resolve()) as slackAttachment:
                data = json.loads(slackAttachment.read())

                data["attachments"][0]["footer"] = "{} | {}]".format(data["attachments"][0]["footer"], datetime.now().strftime('%Y-%m-%d [%H:%M:%S.%f')[:-3])

                print(item.get('webhook')) #Will print swedish or mixed
                print(isTrueFalse) #Will print either True or False

                #If item.get('webhook') is mixed and isTrueFalse is True -> print discordFiltered Mixed first URL (Then second URL on next loop)
                #If item.get('webhook') is mixed and isTrueFalse is False -> print discordUnfiltered Mixed first URL (Then second URL on next loop)
                #If item.get('webhook') is swedish and isTrueFalse is True -> print discordFiltered Swedish first URL (Then second URL on next loop)
                #If item.get('webhook') is swedish and isTrueFalse is False -> print discordUnfiltered Swedish first URL (Then second URL on next loop)

我的问题是,我目前不知道如何正确地执行if语句,也不知道如何从dict中的列表中逐个打印出值

我想知道我是如何做到这些例子的:

#If item.get('webhook') is mixed and isTrueFalse is True -> print discordFiltered Mixed first URL (Then second URL on next loop)
#If item.get('webhook') is mixed and isTrueFalse is False -> print discordUnfiltered Mixed first URL (Then second URL on next loop)
#If item.get('webhook') is swedish and isTrueFalse is True -> print discordFiltered Swedish first URL (Then second URL on next loop)
#If item.get('webhook') is swedish and isTrueFalse is False -> print discordUnfiltered Swedish first URL (Then second URL on next loop)

Tags: andhttpscomloopapiurlgetis
1条回答
网友
1楼 · 发布于 2024-04-25 23:12:20

我可能完全误解了你的问题。如果是,请发表评论,我将重新尝试一个解决方案或删除这个。你知道吗

循环外部有一个初始化为0的变量url\u索引。循环结束时,递增此变量。假设循环次数不会超过两次,所以这个变量的值是0,然后是1。你知道吗

key = item.get('webhook') # 'swedish' or 'mixed'
if isTrueFalse:
    discordFilteredList = self.discordFiltered[key]
    print(discordFilteredList[url_index])
else:
    discordUnfilteredList = self.discordUnfiltered[key]
    print(discordUnfilteredList[url_index])

以上可以更简洁地表达,但我想说清楚。你知道吗

相关问题 更多 >