通过修改python中的现有词典来创建新词典

通过修改python中的现有词典来创建新词典,python,dictionary,extend,Python,Dictionary,Extend,我有一份口述如下 querydict = {u'variantcategoryitem_formset_0-0-variant_category_item_price': [u''], u'variantcategoryitem_formset_0-0-variant_category_item_quantity': [u''], u'variantcategoryitem_formset_0-0-variant_category_item_name': [u'hurray'], } 因此,从

我有一份口述如下

querydict = {u'variantcategoryitem_formset_0-0-variant_category_item_price': [u''], u'variantcategoryitem_formset_0-0-variant_category_item_quantity': [u''], u'variantcategoryitem_formset_0-0-variant_category_item_name': [u'hurray'], }
因此,从上面的字典中,如果没有
price,quantity
字段的值,我应该将值添加为
0(数量),0.0(价格)

如下

new_dict = {}
for key, value in querydict.iteritems():
    # Need to check variant_category_item_qunatity or variant_category_item_price exists in the key, because as u can observe there will be many quantity, price fields in the querydict like variantcategoryitem_formset_0-0-variant_category_item_price, variantcategoryitem_formset_1-1-variant_category_item_price, etc.

    if 'variant_category_item_qunatity' in key:
        # If key exists and if equal to ''(empty string) update the new-dict with this key and value as 0(same in case of price below) 
        if not querydict[key]:
            new_dict[key] = 0
    elif 'variant_category_item_price' in key:
        if not querydict[key]:
            new_dict[key] = 0.0
    # Update the entire new_dict with remaining values
    new_dict[key] = value

但它不起作用,我可以在没有数量和价格的情况下看到字典,因此,如果在
querydict
中价格和数量是
'
,那么任何人都可以更正上述逻辑并通过将价格和数量更新为0.0到0来使用querydict值创建新的dict吗?

问题是
querydict
包含一个空字符串的列表,因此
如果不是querydict[key]
始终计算为
False
,因为包含一项的列表不是虚假值

>>> bool([u''])
True
您应将条件更改为:

if querydict[key] == [u'']
  • 其次,在循环中,您总是覆盖if-elif条件中设置的值,因此将最后一条语句放在
    else
    块中

  • 最后,您在
    “变体\u类别\u项目\u数量”中有一个输入错误

工作版本:

querydict = {u'variantcategoryitem_formset_0-0-variant_category_item_price': [u''], u'variantcategoryitem_formset_0-0-variant_category_item_quantity': [u''], u'variantcategoryitem_formset_0-0-variant_category_item_name': [u'hurray'], }
new_dict = {}
for key, value in querydict.iteritems():

    if 'variant_category_item_quantity' in key: #Typo in quantity spelling in your code
        if querydict[key] == [u'']:
            new_dict[key] = 0

    elif 'variant_category_item_price' in key:
        if querydict[key] == [u'']:
            new_dict[key] = 0.0

    # You need an else condition here, otherwise you'll overwrite the
    # values set in if-elif's         
    else:
         new_dict[key] = value
print new_dict
几个问题:

  • 您在第一个
    处有打字错误,如果在
    变体(分类)项目(数量)
    处有打字错误
    则应为
    数量

  • 您的
    项是未签名字符串的列表,因此必须与正确的类型进行比较

  • 我建议对dict使用
    update()
    ,这样更容易理解

  • 解决方案如下:

    querydict = {u'variantcategoryitem_formset_0-0-variant_category_item_price': [u''], u'variantcategoryitem_formset_0-0-variant_category_item_quantity': [u''], u'variantcategoryitem_formset_0-0-variant_category_item_name': [u'hurray'], }
    
    
    new_dict = {}
    
    for key, value in querydict.iteritems():
        # Need to check variant_category_item_qunatity or variant_category_item_price exists in the key, because as u can observe there will be many quantity, price fields in the querydict like variantcategoryitem_formset_0-0-variant_category_item_price, variantcategoryitem_formset_1-1-variant_category_item_price, etc.
    
        if 'variant_category_item_quantity' in key:
            # If key exists and if equal to ''(empty string) update the new-dict with this key and value as 0(same in case of price below)
            if querydict[key] == [u'']:
                new_dict.update({key: 0})
        elif 'variant_category_item_price' in key:
            if querydict[key] == [u'']:
                new_dict.update({key: 0.0})
        # Update the entire new_dict with remaining values
        else:
            new_dict.update({key:value})
    
    print new_dict
    
    {u'variantcategoryitem_formset_0-0-variant_category_item_name': [u'hurray'], u'variantcategoryitem_formset_0-0-variant_category_item_price': 0.0, u'variantcategoryitem_formset_0-0-variant_category_item_quantity': 0}
    
    输出:

    querydict = {u'variantcategoryitem_formset_0-0-variant_category_item_price': [u''], u'variantcategoryitem_formset_0-0-variant_category_item_quantity': [u''], u'variantcategoryitem_formset_0-0-variant_category_item_name': [u'hurray'], }
    
    
    new_dict = {}
    
    for key, value in querydict.iteritems():
        # Need to check variant_category_item_qunatity or variant_category_item_price exists in the key, because as u can observe there will be many quantity, price fields in the querydict like variantcategoryitem_formset_0-0-variant_category_item_price, variantcategoryitem_formset_1-1-variant_category_item_price, etc.
    
        if 'variant_category_item_quantity' in key:
            # If key exists and if equal to ''(empty string) update the new-dict with this key and value as 0(same in case of price below)
            if querydict[key] == [u'']:
                new_dict.update({key: 0})
        elif 'variant_category_item_price' in key:
            if querydict[key] == [u'']:
                new_dict.update({key: 0.0})
        # Update the entire new_dict with remaining values
        else:
            new_dict.update({key:value})
    
    print new_dict
    
    {u'variantcategoryitem_formset_0-0-variant_category_item_name': [u'hurray'], u'variantcategoryitem_formset_0-0-variant_category_item_price': 0.0, u'variantcategoryitem_formset_0-0-variant_category_item_quantity': 0}
    

    我也尝试了上述条件,仍然得到了价格为[''的字典,数量为['']