An efficient, pythonic way to calculate result sets

happyhondje at gmail.com happyhondje at gmail.com
Fri Oct 26 15:59:23 CEST 2007


> def cross_nodups(*args):
>     'Cross product after eliminating repeat elements, keeping constant
> size'
>     ans = [[]]
>     for arg in args:
>         ans = [x+[y] for x in ans for y in arg if y not in x]
>     return ans
>
> def choose_first(obj1, *args):
>     'Assume a choice of a first object'
>     return cross_nodups(frozenset((obj1,)), *args[1:])

Oops, crap, I pasted the unchanged cross_nodups() you wrote. My
adjusted variety:

def cross_nodups(*args):
    'Cross product after eliminating repeat elements, keeping constant
size'
    ans = [[]]
    for arg in args:
        ans = [frozenset(list(x)+[y]) for x in ans for y in arg if y
not in x]
    return set(ans)

Anyhow, thank you! :)




More information about the Python-list mailing list