Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.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列表_Python_String_List_Dictionary_Set - Fatal编程技术网

用Python打印dict列表

用Python打印dict列表,python,string,list,dictionary,set,Python,String,List,Dictionary,Set,我有以下清单: l = [[{'close': 'TRUE'}], [{'error': 'FALSE'}], [{'close': 'TRUE', 'error': 'TRUE'}, {'close': 'TRUE', 'error': 'FALSE'}]] 我想这样打印: (close = TRUE) & (error = FALSE) & ((close = TRUE & error = TRUE) | (close = TRUE & error = FA

我有以下清单:

l = [[{'close': 'TRUE'}], [{'error': 'FALSE'}], [{'close': 'TRUE', 'error': 'TRUE'}, {'close': 'TRUE', 'error': 'FALSE'}]]
我想这样打印:

(close = TRUE) & (error = FALSE) & ((close = TRUE & error = TRUE) | (close = TRUE & error = FALSE))
目前,我有以下功能几乎可以完成这项工作:

def pretty_string(l):
    print ' & '.join('({0})'
                        .format(' | '
                                .join('({0})'
                                      .format(' & '
                                              .join('{0} = {1}'
                                                    .format(key, value)
                                                    for key, value
                                                    in disjuncts.items()))
                                      for disjuncts
                                      in conjuncts))
                        for conjuncts
                        in l)
但它给了我:

((close = TRUE)) & ((error = FALSE)) & ((close = TRUE & error = TRUE) | (close = TRUE & error = FALSE))
注意“(close=TRUE)”和“(error=FALSE)”周围的额外括号


如何删除它们?

只需使用
if
语句根据内部列表的长度更改格式字符串(
(“({0})”if len(disjuncts)>1 else“{0}”)
)。像这样:

def pretty_string(l):
    print ' & '.join(
        '({0})'.format(
            ' | '.join(
                ('({0})' if len(disjuncts) > 1 else '{0}').format(
                    ' & '.join(
                        '{0} = {1}'.format(key, value) for key, value in disjuncts.items()
                    )
                ) for disjuncts in conjuncts
            )
        ) for conjuncts in l
    )

只需使用
if
语句根据内部列表的长度更改格式字符串(
(“({0})”if len(disjuncts)>1或“{0}”)
)。像这样:

def pretty_string(l):
    print ' & '.join(
        '({0})'.format(
            ' | '.join(
                ('({0})' if len(disjuncts) > 1 else '{0}').format(
                    ' & '.join(
                        '{0} = {1}'.format(key, value) for key, value in disjuncts.items()
                    )
                ) for disjuncts in conjuncts
            )
        ) for conjuncts in l
    )