How do I find the Subsets of given Set in Sage?
Question: I have code that looks like this:

    
    
    def Z(m,n):
        return CartesianProduct(IntegerRange(m),IntegerRange(n))
    
    for v in Subsets(Z(2,2)):
        print v
    

However, when I try to run it, I get the following error:

    
    
    Traceback (most recent call last):
      File "x.py", line 13, in <module>
        for v in Subsets(Z(_sage_const_2 ,_sage_const_2 )):
      File "/opt/sage-4.8-linux-64bit-ubuntu_10.04.3_lts-x86_64-Linux/local/lib/python2.6/site-packages/sage/combinat/subset.py", line 234, in __iter__
        lset = __builtin__.list(self.s)
      File "/opt/sage-4.8-linux-64bit-ubuntu_10.04.3_lts-x86_64-Linux/local/lib/python2.6/site-packages/sage/sets/set.py", line 650, in __iter__
        for x in self.set():
      File "/opt/sage-4.8-linux-64bit-ubuntu_10.04.3_lts-x86_64-Linux/local/lib/python2.6/site-packages/sage/sets/set.py", line 719, in set
        return set(self.object())
    TypeError: unhashable type: 'list'
    

What is the canonical way of getting the set of all subsets of an arbitrary
Set?


Answer: `CartesianProduct` returns a list of lists, e.g.:

    
    
    >>> print list(Z(2,2))
    [[0, 0], [0, 1], [1, 0], [1, 1]]
    

But `Subsets` can't cope with the elements being lists (it converts its
argument to a `set` internally, and a set in Python is implemented as a hash
set, hence the error about "unhashibility"). To fix this, you should convert
the internal lists to tuples:

    
    
    for v in Subsets(tuple(l) for l in Z(2,2)):
        print v
    

Note that this is using a generator expression (rather than a list
comprehension) to avoid constructing an intermediate list.

(One could also use `map(tuple, Z(2,2))` or `import itertools`
`iterools.imap(tuple, Z(2,2))` in place of the generator expression, but the
solution given above is the most Pythonic.)

