(states)
return statelist
For each item of states return a list containing all combinations of
    that item with individual bits reset, sorted by the number of set bits.
For each item of states return a list containing all combinations of
    that item with individual bits reset, sorted by the number of set bits.
For each item of states return a list containing all combinations of that item with individual bits reset sorted by the number of set bits .
def expand_substates(states):
    '''For each item of states return a list containing all combinations of
    that item with individual bits reset, sorted by the number of set bits.
    '''
    def nbits(n):
        "number of bits set in n base 2"
        nb = 0
        while n:
            n, rem = divmod(n, 2)
            nb += rem
        return nb
    statelist = []
    for state in states:
        substates = list(set(state & x for x in states))
        substates.sort(key=nbits, reverse=True)
        statelist.append(substates)
    return statelist
def expand_substates ( states ) : def nbits ( n ) : "number of bits set in n base 2" nb = 0 while n : n , rem = divmod ( n , 2 ) nb += rem return nb statelist = [ ] for state in states : substates = list ( set ( state & x for x in states ) ) substates . sort ( key = nbits , reverse = True ) statelist . append ( substates ) return statelist