Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To list all functions from A to B you must iterate on all points of B^|A|. This can be achieved with itertools' 'product' function:

def all_functions(A,B):
    from itertools import product
    B_to_the_A = product(B,repeat=len(A))
    for p in B_to_the_A:
        yield dict(zip(A,p))

sage: for f in all_functions("abc",[0,1]):
....:     print f
{'a': 0, 'c': 0, 'b': 0}
{'a': 0, 'c': 1, 'b': 0}
{'a': 0, 'c': 0, 'b': 1}
{'a': 0, 'c': 1, 'b': 1}
{'a': 1, 'c': 0, 'b': 0}
{'a': 1, 'c': 1, 'b': 0}
{'a': 1, 'c': 0, 'b': 1}
{'a': 1, 'c': 1, 'b': 1}

Nathann