使用在列表理解中创建的变量

时间:2019-03-06 02:20:51

标签: python variables list-comprehension

我想使用在 [HttpPost] [ValidateAntiForgeryToken] public async Task<ActionResult> DemoPage(ContactVM contactInfo) { try { SendMail(contactInfo).Wait(); ViewBag.PostEmailMsg = "Your message has been sent."; } catch (Exception ex) { ViewBag.PostEmailErr = "We are currently unable to deliver your message. Please try again later. We apologize for the inconvenience."; } ViewBag.JumpToID = "contact"; return View(contactInfo); } static async Task SendMail(ContactVM contactInfo) { var apiKey = ConfigurationManager.AppSettings["sgapi"]; var sgClient = new SendGridClient(apiKey); var from = new EmailAddress(contactInfo.Email, contactInfo.Name); var subject = contactInfo.MsgSubject; var to = new EmailAddress(ConfigurationManager.AppSettings["emailTo"] ); var plainTextContent = contactInfo.MsgText; var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, ""); var response = await sgClient.SendEmailAsync(msg); } 中创建的变量“ i”,但是当我尝试在行poss = [j.split() for j in [decipher(i, txt) for i in range(1, 26)]]中访问它时,它会引发NameError。有什么办法可以做到而又不会引发错误?

return f"The key is {i} and the deciphered text is{' '.join(k)}."

1 个答案:

答案 0 :(得分:0)

您可以将i的值及其相应的元素保存起来,然后在对之间循环:

popular = open("C:/Users/NAME/FilesForPyCharm/popular.txt")
words = [line.strip() for line in popular]
poss = [(j.split(), i) for j in [decipher(i, txt) for i in range(1, 26)]]
for k, i in poss:
    if set(k).issubset(set(words)):
        return f"The key is {i} and the deciphered text is{' '.join(k)}."