-2

Sorry to bother you with my stupidity... I can't find how itertools.permutations work, the output is strange ! It is supposed to return all the possible combinations, like a bruteforce with a length defined for the output (a, b, c, ..., aa, ab, ac..., aaa, aab, aac... is a bruteforce, and permutations with 3 for exemple as length : aaa, aab, aac... dee, def, deg, ... zaa, zab, zac...), isn't it ?

But if I use "0123456789", sometimes values are missing, like 040613, and a lot are missing even if globally I got a loooot of values. So how does it work ?

If you want specifications, I try to get all possible dates at the format 050520 for exemple (may the second, 2020). But if dates are missing...

My code :

from itertools import permutations #or combinations
def dates(charset, maxlength):
    return (''.join(candidate) for candidate in permutations(charset, maxlength))
print(list(dates("0123456789", 6)))

Thanks to all who will try to help me, have a nice day !

Quantum Sushi
  • 504
  • 7
  • 23
  • `'040613'` is not a permutation at all, so why should `itertools.permutations` generate it? My guess is that you want `itertools.product('0123456789',repeat = 6)` – John Coleman May 02 '20 at 14:29
  • 1
    "supposed to return all the possible combinations" - then you need to use `itertools.combinations`. – mkrieger1 May 02 '20 at 14:31
  • Do I want to bang my head realising how stupid I am ? Absolutely. – Quantum Sushi May 02 '20 at 14:31
  • `aaa, aab, aac... dee, def, deg, ... zaa, zab, zac...` are not permutations with 3 for `'abcdefghijklmnopqrstuvwxyz'`. – Austin May 02 '20 at 14:32
  • @Austin yeah that was stupid from me but I took for exemple charset = "abcdefghijklmnopqrstuvwxyz" and then I use numbers ^^' – Quantum Sushi May 02 '20 at 14:32

1 Answers1

2

The permutations of "0123456789" do not include "040613" since this number requires 2 of the zero token.

For example:

>>> list(permutations("123", 3)) [('1', '2', '3'), ('1', '3', '2'), ('2', '1', '3'), ('2', '3', '1'), ('3', '1', '2'), ('3', '2', '1')] Does not contain "333" or "313"

leon
  • 45
  • 4
  • You could expand your `charset`, which would create a lot more iteration cycles, and then filter these. Better solutions exist: https://stackoverflow.com/a/993367/9592055 `base = datetime.datetime.today() date_list = [base - datetime.timedelta(days=x) for x in range(numdays)]` – leon May 02 '20 at 14:33