简体   繁体   中英

get all 4 digit combinations out of a list of strings

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 (ie: if I am using '8800' to generate it this number should not be in the list)

Thank you!

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM