python - 如何在字典内的列表中逐个打印值
问题描述
所以我一直在试图弄清楚如何一个一个地打印一个位于字典内的列表中的值。我将在它的外观下面发布一个代码,并发表评论。
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)
解决方案
我可能完全误解了你的问题。如果是这样,请发表评论,我将重新尝试解决方案或删除此解决方案。
在循环之外有一个变量 url_index 初始化为 0。在循环结束时增加这个变量。假设我们不会循环超过两次,所以这个变量将取值 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])
以上可以更简洁地表达,但我希望它清楚。
推荐阅读
- java - android打开应用时所有通知都消失了
- python - Pandas:在给定条件下添加具有均匀间隔时间戳的新行
- php - 从数组访问对象返回未定义
- git - Git - 如何找到在特定 git 分支中完成的实际提交
- django - 如何在表单提交之前将多个图像异步添加到 django 表单
- javascript - chrome.webRequest.onBeforeRequest 导致网页部分功能加载失败
- amazon-web-services - 随机切割森林的超参数调整
- java - 黑白 ChromeDriver 和 WebDriver 有什么区别?
- python - 使用 raw_input 不能输入超过 1025 个字符
- kubernetes - 在定义集合时添加新闻行