【问题标题】:How to iterate over several list at the same time?如何同时遍历多个列表?
【发布时间】:2012-05-26 21:08:00
【问题描述】:

这里有个谜 我想要做的是有一个“for in”,它循环来自 request.POST 和 request.FILES 的 3 个变量 我所做的是:

images = request.FILES.getlist('image')
titles = request.POST.getlist('title')
captions = request.POST.getlist('caption')
for image,title,caption in images,titles,captions:

这似乎不起作用 有什么解决办法吗?

【问题讨论】:

  • @Acorn 所说的使用 zip 时,请记住(引用文档):返回的列表在长度上被截断为最短参数序列的长度。
  • +1 用于短语“with for in”

标签: python django django-views python-2.7


【解决方案1】:
for image, title, caption in zip(images, titles, captions):

..是你想要的。 (zip docs)

【讨论】:

  • 很抱歉打扰你,但如果我想得到标题字段中写的内容,我应该写什么?
  • @omar-mowafi 欢迎来到 StackOverflow!如果答案对你有用,你应该“接受”它。这给了回答者一个声誉分数,并提供了在这里回答问题的动机。如果您的“接受”率较高,人们将更有可能为您提供帮助。
猜你喜欢
  • 2015-12-07
  • 2011-08-03
  • 2021-11-29
  • 2021-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多