用for循环填充字典

时间:2015-05-05 11:07:03

标签: python dictionary

我有几个大词典,除最后几个字符外,所有值都相同。

喜欢:http://www:example.com/abc

现在我正在使用这样的词典:

categories = {1:'http://www:example.com/abc',
              2:'http://www:example.com/def'

另外30 k,v对。

如何使用for循环将静态和结束变量一起添加为值,并生成整数作为字典的键?

static = 'http://www.example.com

end = ['abc','def']

2 个答案:

答案 0 :(得分:2)

你可以用词典理解来做你想做的事。

  $(Qweb.render("fb_shared_post", {'res':'Hello'})).prependTo('<TemplateDivID or Class>');

但它确实提出了@mkrieger提出的问题,为什么不只是使用列表。

答案 1 :(得分:1)

使用词典理解。

$(function () {   $('[data-toggle="popover"]').popover() })

由于键是一系列整数,因此您也可以使用列表。唯一的区别是索引从0开始而不是1。

template = 'http://www.example.com/{path}'
categories = {i+1: template.format(path=e) for i, e in enumerate(end)}
相关问题