简体   繁体   中英

create a new dictionary by modifying the existing dictionary in python

I have a dict as below

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'], }

So from the above dictionary if it does n't has values for price, quantity fields, i should add the vales as 0(quantity), 0.0(price)

like below

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

But its not working, i can able to see the dictionary without quantity and price values, so can anyone correct the above logic and create the new dict with the querydict values by updating the price and quantity to 0.0 to 0 if they are '' in querydict ?

Problem is querydict contains a list with empty string, so if not querydict[key] always evaluates to False because a list with one item is not a falsy value.

>>> bool([u''])
True

You should change the condition to:

if querydict[key] == [u'']
  • Secondly in your loop you always overwrite the value set in if-elif conditions, so put that last statement in an else block.

  • Lastly you've a typo in 'variant_category_item_qunatity' :

Working version:

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

Few problems:

  1. you have typo at the first if at variant_category_item_qunatity it should be quantity

  2. your item is list of unsigned string so you must compare to the right type.

  3. I recommend to use update() with dict, it's just more clear to understand...

Here is the solution:

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

Output:

{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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM