Python, not able to append to a list from a recursive function

Shivendra

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. By solution I mean the set of values of these variables which makes the input expression give 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.

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}] 
JaviHerAr

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. Try to make a copy of the comb_dict:

all_combinations.append(comb_dict.copy())

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Confounding recursive list append in Python

From Dev

How to return a list from a recursive function in Python?

From Dev

Infinite recursive list from function

From Dev

python - return flat list of objects from recursive function

From Dev

Return a flatten list from my recursive function in Python

From Dev

Recursive sorting function for list in Python

From Dev

Python recursive list search function

From Dev

Python recursive list search function

From Dev

Append a list from a search function

From Dev

recursive max function for list of list python

From Dev

Is it possible to use an append function in a return statement for purposes of a recursive procedure in Python

From Dev

Python: using append within recursive function - overwrites previous elements

From Dev

Remove list from the recursive function if it is empty

From Dev

python not being able to access values from list

From Dev

Recursive Python function to produce a list of anagrams

From Dev

Recursive Python function to count occurrences of an element in a list

From Dev

Modifying a list outside of recursive function in Python

From Dev

Recursive function in Python does not change list

From Dev

Append to list while in recursive loop

From Dev

recursive function to return a list of all connected nodes, given a certain node from network graph using python

From Dev

Write recursive data in excel from python list

From Dev

How to return value from recursive function in python?

From Dev

Read a list from a file and append to it using Python

From Dev

append item to variable list from widget in python

From Dev

Python append words to a list from file

From Java

Recursive function for list of lists

From Dev

Recursive function of a list

From Dev

Recursive Function on a List in Haskell

From Dev

Recursive Files list function

Related Related

  1. 1

    Confounding recursive list append in Python

  2. 2

    How to return a list from a recursive function in Python?

  3. 3

    Infinite recursive list from function

  4. 4

    python - return flat list of objects from recursive function

  5. 5

    Return a flatten list from my recursive function in Python

  6. 6

    Recursive sorting function for list in Python

  7. 7

    Python recursive list search function

  8. 8

    Python recursive list search function

  9. 9

    Append a list from a search function

  10. 10

    recursive max function for list of list python

  11. 11

    Is it possible to use an append function in a return statement for purposes of a recursive procedure in Python

  12. 12

    Python: using append within recursive function - overwrites previous elements

  13. 13

    Remove list from the recursive function if it is empty

  14. 14

    python not being able to access values from list

  15. 15

    Recursive Python function to produce a list of anagrams

  16. 16

    Recursive Python function to count occurrences of an element in a list

  17. 17

    Modifying a list outside of recursive function in Python

  18. 18

    Recursive function in Python does not change list

  19. 19

    Append to list while in recursive loop

  20. 20

    recursive function to return a list of all connected nodes, given a certain node from network graph using python

  21. 21

    Write recursive data in excel from python list

  22. 22

    How to return value from recursive function in python?

  23. 23

    Read a list from a file and append to it using Python

  24. 24

    append item to variable list from widget in python

  25. 25

    Python append words to a list from file

  26. 26

    Recursive function for list of lists

  27. 27

    Recursive function of a list

  28. 28

    Recursive Function on a List in Haskell

  29. 29

    Recursive Files list function

HotTag

Archive