Posts: 4,646
Threads: 1,493
Joined: Sep 2016
i need to create permutations of N letters. but i also need
all lengths in range(1,N+1), N just happens to also be the number of letters to be permuted?
what is this kind of permutation called? is there a way to do this in
itertools? or do i need to
chain a bunch of
permute() iterators of varying length? i know a way to do this but i am unsure of the best way in Python.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,646
Threads: 1,493
Joined: Sep 2016
Feb-18-2022, 12:02 AM
(This post was last modified: Feb-18-2022, 12:03 AM by Skaperen.)
i was using chain().
if there is a more modern term, i'd expect wikipedia to have it.
i'm also doing this as a combination of two sets of permutations to make a variety of command symlinks. here is the code i have done, so far:
from itertools import chain, permutations
s1 = 'eio' # characters for name base
s2 = 'al' # characters for name suffix
# combine all permutations of base and suffix
# and also without a suffix
# these combinations are excluded
ex = ('e','eio')
def vlp(s):
"""Varying length permutations chained together, 1 to max (inclusive)."""
return chain(*[permutations(s,n)for n in range(1,len(s)+1)])
a = [x for x in vlp(s1)]
b = [()] + [x for x in vlp(s2)] # include [()] so there is an empty case in suffixes
p = [x+y for x in a for y in b] # join all combinations including the empty suffix
n = [''.join(x) for x in p if ''.join(x) not in ex] # make strings and do excludes
# print all the nammes and how many
print(' '.join(sorted(n)))
print(len(n))
i'd like to find a way to do that comprehension in line 19 coding
''.join(x)
only once.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,646
Threads: 1,493
Joined: Sep 2016
it works, but i will need to study it a while to understand all the changes.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.