4
\$\begingroup\$

I'm trying to find an alternative way to write this code such as using different ways to achieve checking if it's a subset and insertion.

def candidates(table, candidates, minsupp):
    count, supported = {}, {}
    for item in table:
        for c in candidates:
            if c.issubset(item): 
                count[c] =  count.get(c, 0) 
                count[c] = count[c] + 1 

    total = float(len(table))
    l_sub_one = []
    for c in count:
        support = count[c] / total 
        if support >= minsupp:
            l_sub_one.insert(0, c)
        supported[c] = support
    return l_sub_one, supported
\$\endgroup\$
1
  • 2
    \$\begingroup\$ It looks like all the information in l_sub_one is in supported, given minsupp, so that you should only construct supported in this function. (Btw, it would be nice to specify your argument types and perhaps say something about the problem domain.) \$\endgroup\$
    – Alan
    Dec 14, 2014 at 22:15

1 Answer 1

1
\$\begingroup\$

As @Alan suggested in his comment, this method is doing 2 different things that can be separated into 2 methods.

This looks strange:

            count[c] =  count.get(c, 0) 
            count[c] = count[c] + 1

Why not:

            count[c] = count.get(c, 0) + 1
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.