带元组的字典

时间:2019-02-19 11:34:03

标签: python python-2.7

我需要通过代码重新创建以下内容:

publications = {"publicationId": "gid://shopify/Publication/17878646899"}, {
                "publicationId": "gid://shopify/Publication/18034294899"}

类似这样的东西:

shopify_channel_names = "17878646899;18034294899"
shopify_channel_names_split = shopify_channel_names.split(';')
for count, shopify_channel in enumerate(shopify_channel_names_split):
    print shopify_channel
    got_publication = {"publicationId": "gid://shopify/Publication/" + shopify_channel}

当我拥有出版物时,我不知道如何创建出版物,我可以使用索引ID来做所有事情,但是我不需要。

1 个答案:

答案 0 :(得分:0)

shopify_channel_names = "17878646899;18034294899"
publications = tuple({"publicationId": "gid://shopify/Publication/{}".format(pub_id)} 
                      for pub_id in shopify_channel_names.split(';'))
print publications

输出

({'publicationId': 'gid://shopify/Publication/17878646899'}, {'publicationId': 'gid://shopify/Publication/18034294899'})