小编Shu*_*Shu的帖子

Django数组或列表输出?

我正在拉一组图片网址和各自的标题.我已经尝试创建一个哈希或关联数组,但数据似乎覆盖,所以我最终只得到数组中的最后一项.

例如;

thumbnail_list = []
for file in media:
    thumbnail_list['url'] = file.url
    thumbnail_list['title'] = file.title
Run Code Online (Sandbox Code Playgroud)

我甚至试过创建两个列表并将它们放在一个更大的列表中.

thumbnail_list.append('foo')
thumbnail_urls.append('bar')
all_thumbs = [thumbnail_list], [thumbnail_urls]
Run Code Online (Sandbox Code Playgroud)

我正在尝试从这些数据中创建一个链接:

<a href="image-url">image title</a>
Run Code Online (Sandbox Code Playgroud)

我一直在接近,但最终我在django模板中一次循环过多数据或所有数据.

想法?

编辑:也许zip()是我需要的?

questions = ['name', 'quest', 'favorite color']
answers = ['lancelot', 'the holy grail', 'blue']
for q, a in zip(questions, answers):
    print 'What is your {0}?  It is {1}.'.format(q, a)
Run Code Online (Sandbox Code Playgroud)

python django hash associative-array

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

associative-array ×1

django ×1

hash ×1

python ×1