0
votes

I have a list consisting of 4 digit strings (ie: list = ['0000', '1111', ...]) now I want to get a list of all possible 4 digit combinations (w/o rep) for each of these items

This is the code I have using permutations (of course it has repetitions but I will post this one since when I tried combinations it worked really bad)

permutes = defaultdict(list)
for item in funo_clean: ## funo_clean is the list with 4 digit numbers
    for permutation in list(itertools.permutations(item)):
        permutes[item].append("".join([str(x) for x in permutation]))
    print ("\n")

Also, I don't want the list of combinations to contain the same number I am using to generate it (i.e: if I am using '8800' to generate it this number should not be in the list)

Thank you!

1
Have you tried itertools.combinations?John Gordon
@JohnGordon Yes, but results were not good (maybe I am not using it right) that's why I posted the code using permutations since it worked better to my goals.SJPRO
Show us what you tried, and explain why the results weren't what you wanted, and maybe we can help.John Gordon
only difference is the change in itertools.permutations(item) to itertools.combinations (item, len(item)) but with the permutations code I get, for example: 0647 => [0647, 0674, 0467, ...] ## And all possible permutations with the combinations code I just get one item inside list 0647 => [0647]SJPRO

1 Answers

0
votes

set the permutations:

permutes = {}
for item in funo_clean:
    permutes[item] = [''.join(permutation) for permutation in set(itertools.permutations(item))]
    permutes[item].remove(item)
    permutes[item].sort()