Skip to main content
added 4 characters in body
Source Link
65Fbef05
  • 419
  • 2
  • 12

I should preface with the fact that I don't know python. It's likely that I've made a very clear error in my following edit. Generally though, I prefer your second function to your first. If python allows, I would break it up a bit for readability. Why do I like it? It doesn't need the yield keyword and it's less nested.

# I like using verbs for functions, perhaps combine_by_subset()
# or generate_subset_list() ?
def combinations_by_subset(seq, r):
    return (

        # I don't know what chain does - I'm making a wild guess that
        # the extra parentheses and comma are unnecessary
        # Could this be reduced to just itertools.chain(c, seq[i]) ?

        tuple(itertools.chain(c, (seq[i], )))
            for i in xrange(r - 1, len(seq))
            for c in combinations_by_subset(seq[:i], r - 1)

        )

  if r

  if r else ( () for i in xrange(1) )

I'm just waiting for this answer to get flagged as not being helpful. Serves me right for putting my two cents in when I don't even know the language. ;)

I should preface with the fact that I don't know python. It's likely that I've made a very clear error in my following edit. Generally though, I prefer your second function to your first. If python allows, I would break it up a bit for readability. Why do I like it? It doesn't need the yield keyword and it's less nested.

# I like using verbs for functions, perhaps combine_by_subset()
# or generate_subset_list() ?
def combinations_by_subset(seq, r):
    return (

        # I don't know what chain does - I'm making a wild guess that
        # the extra parentheses and comma are unnecessary
        # Could this be reduced to just itertools.chain(c, seq[i]) ?

        tuple(itertools.chain(c, (seq[i], )))
        for i in xrange(r - 1, len(seq))
        for c in combinations_by_subset(seq[:i], r - 1)

        )

     if r else ( () for i in xrange(1) )

I'm just waiting for this answer to get flagged as not being helpful. Serves me right for putting my two cents in when I don't even know the language. ;)

I should preface with the fact that I don't know python. It's likely that I've made a very clear error in my following edit. Generally though, I prefer your second function to your first. If python allows, I would break it up a bit for readability. Why do I like it? It doesn't need the yield keyword and it's less nested.

# I like using verbs for functions, perhaps combine_by_subset()
# or generate_subset_list() ?
def combinations_by_subset(seq, r):
    return (

        # I don't know what chain does - I'm making a wild guess that
        # the extra parentheses and comma are unnecessary
        # Could this be reduced to just itertools.chain(c, seq[i]) ?

        tuple(itertools.chain(c, (seq[i], )))
            for i in xrange(r - 1, len(seq))
            for c in combinations_by_subset(seq[:i], r - 1)

        ) if r

    else ( () for i in xrange(1) )

I'm just waiting for this answer to get flagged as not being helpful. Serves me right for putting my two cents in when I don't even know the language. ;)

Source Link
65Fbef05
  • 419
  • 2
  • 12

I should preface with the fact that I don't know python. It's likely that I've made a very clear error in my following edit. Generally though, I prefer your second function to your first. If python allows, I would break it up a bit for readability. Why do I like it? It doesn't need the yield keyword and it's less nested.

# I like using verbs for functions, perhaps combine_by_subset()
# or generate_subset_list() ?
def combinations_by_subset(seq, r):
    return (

        # I don't know what chain does - I'm making a wild guess that
        # the extra parentheses and comma are unnecessary
        # Could this be reduced to just itertools.chain(c, seq[i]) ?

        tuple(itertools.chain(c, (seq[i], )))
        for i in xrange(r - 1, len(seq))
        for c in combinations_by_subset(seq[:i], r - 1)

        )

    if r else ( () for i in xrange(1) )

I'm just waiting for this answer to get flagged as not being helpful. Serves me right for putting my two cents in when I don't even know the language. ;)