简体   繁体   English

Python,无法从递归函数追加到列表

[英]Python, not able to append to a list from a recursive function

I am in the mid-way of writing a code to find all possible solutions of a input similar like "a&b|c!d|a" , where a,b,c,d all are booleans and &- and , |- or !- not are the operators. 我正在编写代码的过程中,以查找类似"a&b|c!d|a"之类的输入的所有可能解决方案,其中a,b,c,d均为布尔值,且&- and &,| | or !- not操作员。 By solution I mean the set of values of these variables which makes the input expression give True . 解决方案是指这些变量的值集,这些值使输入表达式为True

I am able to print all possible combinations of the variables, but I am not able to retain them (in this case, in a list) for later use. 我可以打印所有可能的变量组合,但无法保留它们(在这种情况下,在列表中)以备后用。

What's wrong in the way I am doing it? 我这样做的方式有什么问题? Are there better ways to store them? 有更好的存储方式吗? generate_combination is the method in which I am trying to do this. generate_combination是我尝试执行此操作的方法。

Code: 码:

import operator

# global all_combinations
all_combinations=[]
answers=[]

def solve(combination, input, rank):
    try:
        substituted_str=""
        for i in input:
            if i in combination:
                substituted_str+=combination[i]
            else:
                substituted_str+=i
        print substituted_str
        # for item in rank:
    except:
        pass

def generate_combination(variables,comb_dict, length, current_index):
    if len(comb_dict)==length:
        print comb_dict #Each combination , coming out right
        all_combinations.append(comb_dict)
        print all_combinations,"\n" #This is not working as expected
    else:
        for i in [1,0]:
            comb_dict[variables[current_index]]=i
            generate_combination(variables,comb_dict, length,current_index+1)
            comb_dict.pop(variables[current_index], None)


def main(input,variables,order):
    rank=sorted(order.items(), key=operator.itemgetter(1))

    generate_combination(variables, {}, len(variables), 0)

    for combination in all_combinations:
        print combination
        ans=solve(combination, input, rank)
        ans=[]
        answers.extend(ans)

    # for answer in answers:
    #     print answer

def nothing():
    pass

if __name__ == '__main__':
    # print "Enter your symbols for :\n"
    # And=raw_input("And = ")
    # Or=raw_input("Or = ")
    # Not=raw_input("Not = ")
    # input_str=raw_input("Enter the expression :")

    And,Or,Not,input_str='&','|','!','a&b|c!d|a'
    input_str=input_str.replace(" ","")


    mapping={And:"&", Or:"|", Not:"!"}
    order={"&":3, "|":2, "!":1}


    variables=[]
    processed_str=""

    for i in input_str:
        if i in mapping:
            processed_str+=mapping[i]
        else:
            processed_str+=i
            variables.append(i)
    variables=list(set(variables))

    print "Reconstituted string : ",processed_str
    print "Variables : ",variables,"\n"

    main(processed_str,variables,order)

Current Output: 电流输出:

Reconstituted string :  a&b|c!d|a
Variables :  ['a', 'c', 'b', 'd'] 

{'a': 1, 'c': 1, 'b': 1, 'd': 1}
[{'a': 1, 'c': 1, 'b': 1, 'd': 1}] 

{'a': 1, 'c': 1, 'b': 1, 'd': 0}
[{'a': 1, 'c': 1, 'b': 1, 'd': 0}, {'a': 1, 'c': 1, 'b': 1, 'd': 0}] 

{'a': 1, 'c': 1, 'b': 0, 'd': 1}
[{'a': 1, 'c': 1, 'b': 0, 'd': 1}, {'a': 1, 'c': 1, 'b': 0, 'd': 1}, {'a': 1, 'c': 1, 'b': 0, 'd': 1}] 

{'a': 1, 'c': 1, 'b': 0, 'd': 0}
[{'a': 1, 'c': 1, 'b': 0, 'd': 0}, {'a': 1, 'c': 1, 'b': 0, 'd': 0}, {'a': 1, 'c': 1, 'b': 0, 'd': 0}, {'a': 1, 'c': 1, 'b': 0, 'd': 0}] 

{'a': 1, 'c': 0, 'b': 1, 'd': 1}
[{'a': 1, 'c': 0, 'b': 1, 'd': 1}, {'a': 1, 'c': 0, 'b': 1, 'd': 1}, {'a': 1, 'c': 0, 'b': 1, 'd': 1}, {'a': 1, 'c': 0, 'b': 1, 'd': 1}, {'a': 1, 'c': 0, 'b': 1, 'd': 1}] 

{'a': 1, 'c': 0, 'b': 1, 'd': 0}
[{'a': 1, 'c': 0, 'b': 1, 'd': 0}, {'a': 1, 'c': 0, 'b': 1, 'd': 0}, {'a': 1, 'c': 0, 'b': 1, 'd': 0}, {'a': 1, 'c': 0, 'b': 1, 'd': 0}, {'a': 1, 'c': 0, 'b': 1, 'd': 0}, {'a': 1, 'c': 0, 'b': 1, 'd': 0}] 

{'a': 1, 'c': 0, 'b': 0, 'd': 1}
[{'a': 1, 'c': 0, 'b': 0, 'd': 1}, {'a': 1, 'c': 0, 'b': 0, 'd': 1}, {'a': 1, 'c': 0, 'b': 0, 'd': 1}, {'a': 1, 'c': 0, 'b': 0, 'd': 1}, {'a': 1, 'c': 0, 'b': 0, 'd': 1}, {'a': 1, 'c': 0, 'b': 0, 'd': 1}, {'a': 1, 'c': 0, 'b': 0, 'd': 1}] 

{'a': 1, 'c': 0, 'b': 0, 'd': 0}
[{'a': 1, 'c': 0, 'b': 0, 'd': 0}, {'a': 1, 'c': 0, 'b': 0, 'd': 0}, {'a': 1, 'c': 0, 'b': 0, 'd': 0}, {'a': 1, 'c': 0, 'b': 0, 'd': 0}, {'a': 1, 'c': 0, 'b': 0, 'd': 0}, {'a': 1, 'c': 0, 'b': 0, 'd': 0}, {'a': 1, 'c': 0, 'b': 0, 'd': 0}, {'a': 1, 'c': 0, 'b': 0, 'd': 0}] 

{'a': 0, 'c': 1, 'b': 1, 'd': 1}
[{'a': 0, 'c': 1, 'b': 1, 'd': 1}, {'a': 0, 'c': 1, 'b': 1, 'd': 1}, {'a': 0, 'c': 1, 'b': 1, 'd': 1}, {'a': 0, 'c': 1, 'b': 1, 'd': 1}, {'a': 0, 'c': 1, 'b': 1, 'd': 1}, {'a': 0, 'c': 1, 'b': 1, 'd': 1}, {'a': 0, 'c': 1, 'b': 1, 'd': 1}, {'a': 0, 'c': 1, 'b': 1, 'd': 1}, {'a': 0, 'c': 1, 'b': 1, 'd': 1}] 

{'a': 0, 'c': 1, 'b': 1, 'd': 0}
[{'a': 0, 'c': 1, 'b': 1, 'd': 0}, {'a': 0, 'c': 1, 'b': 1, 'd': 0}, {'a': 0, 'c': 1, 'b': 1, 'd': 0}, {'a': 0, 'c': 1, 'b': 1, 'd': 0}, {'a': 0, 'c': 1, 'b': 1, 'd': 0}, {'a': 0, 'c': 1, 'b': 1, 'd': 0}, {'a': 0, 'c': 1, 'b': 1, 'd': 0}, {'a': 0, 'c': 1, 'b': 1, 'd': 0}, {'a': 0, 'c': 1, 'b': 1, 'd': 0}, {'a': 0, 'c': 1, 'b': 1, 'd': 0}] 

{'a': 0, 'c': 1, 'b': 0, 'd': 1}
[{'a': 0, 'c': 1, 'b': 0, 'd': 1}, {'a': 0, 'c': 1, 'b': 0, 'd': 1}, {'a': 0, 'c': 1, 'b': 0, 'd': 1}, {'a': 0, 'c': 1, 'b': 0, 'd': 1}, {'a': 0, 'c': 1, 'b': 0, 'd': 1}, {'a': 0, 'c': 1, 'b': 0, 'd': 1}, {'a': 0, 'c': 1, 'b': 0, 'd': 1}, {'a': 0, 'c': 1, 'b': 0, 'd': 1}, {'a': 0, 'c': 1, 'b': 0, 'd': 1}, {'a': 0, 'c': 1, 'b': 0, 'd': 1}, {'a': 0, 'c': 1, 'b': 0, 'd': 1}] 

{'a': 0, 'c': 1, 'b': 0, 'd': 0}
[{'a': 0, 'c': 1, 'b': 0, 'd': 0}, {'a': 0, 'c': 1, 'b': 0, 'd': 0}, {'a': 0, 'c': 1, 'b': 0, 'd': 0}, {'a': 0, 'c': 1, 'b': 0, 'd': 0}, {'a': 0, 'c': 1, 'b': 0, 'd': 0}, {'a': 0, 'c': 1, 'b': 0, 'd': 0}, {'a': 0, 'c': 1, 'b': 0, 'd': 0}, {'a': 0, 'c': 1, 'b': 0, 'd': 0}, {'a': 0, 'c': 1, 'b': 0, 'd': 0}, {'a': 0, 'c': 1, 'b': 0, 'd': 0}, {'a': 0, 'c': 1, 'b': 0, 'd': 0}, {'a': 0, 'c': 1, 'b': 0, 'd': 0}] 

{'a': 0, 'c': 0, 'b': 1, 'd': 1}
[{'a': 0, 'c': 0, 'b': 1, 'd': 1}, {'a': 0, 'c': 0, 'b': 1, 'd': 1}, {'a': 0, 'c': 0, 'b': 1, 'd': 1}, {'a': 0, 'c': 0, 'b': 1, 'd': 1}, {'a': 0, 'c': 0, 'b': 1, 'd': 1}, {'a': 0, 'c': 0, 'b': 1, 'd': 1}, {'a': 0, 'c': 0, 'b': 1, 'd': 1}, {'a': 0, 'c': 0, 'b': 1, 'd': 1}, {'a': 0, 'c': 0, 'b': 1, 'd': 1}, {'a': 0, 'c': 0, 'b': 1, 'd': 1}, {'a': 0, 'c': 0, 'b': 1, 'd': 1}, {'a': 0, 'c': 0, 'b': 1, 'd': 1}, {'a': 0, 'c': 0, 'b': 1, 'd': 1}] 

{'a': 0, 'c': 0, 'b': 1, 'd': 0}
[{'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}] 

{'a': 0, 'c': 0, 'b': 0, 'd': 1}
[{'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}] 

{'a': 0, 'c': 0, 'b': 0, 'd': 0}
[{'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}] 

I think the problem is that all the items in your all_combinations list are pointed to the same comb_dict , you are overwriting each element in every call of generate_combination . 我认为问题在于all_combinations列表中的所有项目都指向相同的comb_dict ,您覆盖了generate_combination的每次调用中的每个元素。 Try to make a copy of the comb_dict : 尝试制作comb_dict的副本:

all_combinations.append(comb_dict.copy())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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