K
K
KPEBETKA2014-02-26 14:18:18
Python
KPEBETKA, 2014-02-26 14:18:18

Itertools.Product: how to cut off unnecessary combinations and determine the number of necessary ones?

I generate all sorts of combinations of characters, but after creating the generator, I have to check that the first and last characters are not '.' and not '-'. Is it possible to somehow get rid of the check condition for the presence of these characters. Those. make sure that at the stage of creating a generator, such combinations are cut off
. If it is possible, then the second question appears. The expression sum([len(stringa)**i for i in rang]) will become irrelevant. And you need to be able to count the right number of combinations

...
rang = range(1, 5)
stringa = 'abcdefghijklmnopqrstuvwxyz1234567890.-'

print '%s combinations ' % sum([len(stringa)**i for i in rang])

for s in rang:
        for comb in itertools.product(stringa, repeat=s):
    if comb[-1] != '.' and comb[0] != '.' and comb[-1] != '-' and comb[0] != '-':
      print ''.join(comb)
...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
leclecovich, 2014-02-26
@KPEBETKA

> similar combinations were cut off
docs.python.org/2/library/string.html#string.strip

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question