我有一段口述如下
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(quantity), 0.0(price)
像下面这样
^{pr2}$但是它不起作用,我可以看到没有数量和价格值的字典,所以如果价格和数量在querydict
中,''
,那么任何人都可以通过将price和quantity更新为0.0到0来更正上面的逻辑并创建新的dict吗?在
问题是
querydict
包含一个空字符串的列表,因此if not querydict[key]
的计算结果总是False
,因为有一个项的列表不是错误的值。在应将条件更改为:
^{pr2}$第二,在循环中,总是重写if elif条件中设置的值,因此将最后一条语句放在
else
块中。最后,您在
'variant_category_item_qunatity'
中出现了一个打字错误:工作版本:
几个问题:
您在第一个
if
在variant_category_item_qunatity
处有打字错误,应该是quantity
您的
item
是无符号字符串的列表,因此必须与正确的类型进行比较。我建议在dict中使用
update()
,这样更容易理解。。。解决方法如下:
输出:
^{pr2}$相关问题 更多 >
编程相关推荐