Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将dict转换为list_Python_Python 2.7 - Fatal编程技术网

Python 将dict转换为list

Python 将dict转换为list,python,python-2.7,Python,Python 2.7,我有一个嵌套的dict,它被解析成一个列表 以下是工作代码: 但有几根牙套不见了 预期结果应该是: [['>=', 'qty', '3'], 'AND', ['in', 'category_ids', '240']] 有人能告诉我哪里出错了吗?将您的查询分配替换为: query = {'type': 'salesrule/rule_condition_combine', 'aggregator': 'all', 'operator': None, 'attribute': None,

我有一个嵌套的dict,它被解析成一个列表

以下是工作代码: 但有几根牙套不见了

预期结果应该是:

[['>=', 'qty', '3'], 'AND', ['in', 'category_ids', '240']]
有人能告诉我哪里出错了吗?

将您的查询分配替换为:

    query = {'type': 'salesrule/rule_condition_combine', 'aggregator': 'all', 'operator': None, 'attribute': None, 
    'conditions': {0: {'type': 'salesrule/rule_condition_product_subselect', 'aggregator': 'all', 'operator': '>=', 'attribute': 'qty', 'is_value_processed': None, 'value': '3'}, 
        1: {'operator': '{}', 'attribute': 'category_ids', 'type': 'salesrule/rule_condition_product', 'is_value_processed': False, 'value': '240'}
    }, 'is_value_processed': None, 'value': '1'}

完整示例

您的查询是嵌套的,我认为您得到的结果将是正确的结果,保持嵌套。但是,如果您想将其展平并删除嵌套,可以使用尾部递归生成结果

这是您的查询:

query = {
    'type': 'salesrule/rule_condition_combine', 
    'aggregator': 'all', 
    'operator': None, 
    'attribute': None, 
    'conditions': {
        0: {
            'type': 'salesrule/rule_condition_product_subselect', 
            'aggregator': 'all', 
            'operator': '>=', 
            'attribute': 'qty', 
            'conditions': {
                0: {
                    'operator': '{}', 
                    'attribute': 'category_ids', 
                    'type': 'salesrule/rule_condition_product', 
                    'is_value_processed': False, 
                    'value': '240'
                    }
                }, 
            'is_value_processed': None, 
            'value': '3'
        }
    }, 
    'is_value_processed': None, 
    'value': '1'
    }
如果您这样构造结果:

import copy
def format_serialized(query, counter, lst):
    counter = counter or 0
    ex = expression_tuple(query)
    if ex:
        lst.append(copy.copy(ex)) # you need to copy the list since it is mutable
    conditions = query.get("conditions")
    if conditions:
        for i in query.get("conditions"):
            if ex:
                lst.append(aggregatorConvert(query.get("aggregator")))
            format_serialized(query.get("conditions")[i], counter + 1, lst)
    return lst
print format_serialized(query, 0, [])
这样称呼它:

import copy
def format_serialized(query, counter, lst):
    counter = counter or 0
    ex = expression_tuple(query)
    if ex:
        lst.append(copy.copy(ex)) # you need to copy the list since it is mutable
    conditions = query.get("conditions")
    if conditions:
        for i in query.get("conditions"):
            if ex:
                lst.append(aggregatorConvert(query.get("aggregator")))
            format_serialized(query.get("conditions")[i], counter + 1, lst)
    return lst
print format_serialized(query, 0, [])
您将获得:

[['>=', 'qty', '3'], 'AND', ['in', 'category_ids', '240']]

这是预期的结果。实现的不同之处在于,您将列表传递到递归函数中,并“向下”而不是“向上”构造它。

只需在
ex.append(aggregatorConvert(query.get(“aggregator”))之前添加
ex=[ex]

if ex:
    ex=[ex]  # add this line
    ex.append(aggregatorConvert(query.get("aggregator")))
ex.append(format_serialized(query.get("conditions")[i], counter + 1)

这将解决问题。

您也可以显示嵌套的dict吗?@Hackaholic在底部的右侧,即“query”变量。没有“额外的括号”-您的列表不是包含字符串和字符串列表,而是包含单个字符串列表和字符串列表。@jornsharpe yes。我应该用正确的措辞。让我编辑这篇文章,因为这是我的预期结果:[['>=','qty','3'],'和',['in','category_ID','240']],这就是我得到的[['>=','qty','3','和',['in','category_ID','240']]。如何实现同样的目标?这有助于您理解问题吗?为什么您期望的是预期结果而不是实际结果?谢谢,但我无法更改查询分配。我无法控制这一切。