I need to know how to get all possible variations with a list without modules. I will be adding a string and I know how to convert it to a list. I did like this:
def dpro(string):
convertedlist = list(string.split(" "))
return convertedlist
print(dpro("this will convert to List"))
This will output a list. I need a loop that will make all different possible outcomes without any modules.
The same process as this question has: Create all possible variations of string Python : but without itertools or any other modules.
CodePudding user response:
The function below will return all iterations of a string without considering spaces.
We iterate of all possible iterations, given by
where N
is the number of characters is the string, and then each iteration is given by the number i
.
The first index (index 1 in maths, index 0
in code) is given by:
The last index (index N in maths, index N-1
in code) is given by:
And the intermediate indices (in maths from 2 to N-1, in code from 1
to N-2
) are given by:
These equations creates a list of indices from which the function can select characters from the original string:
def dpro(string: str):
"""Similar to itertools.product for all characters in a string"""
# get length of string
N = len(string)
# iterate over all iterations given by N ** N
total_iterations = N ** N
for i in range(total_iterations):
# get the first character
first_idx = i // N ** (N - 1)
# get the last character
last_idx = i % N
# get the intermediate characters
inter_idx = [i // N ** (N - 2 - j) % N for j in range(N - 2)]
# create a list of
indices = [first_idx] inter_idx [last_idx]
yield ''.join(string[i] for i in indices)
string = 'abc'
for entry in dpro(string):
print(entry)
This prints the following to the console:
aaa
aab
aac
aba
abb
abc
aca
acb
acc
baa
bab
bac
bba
bbb
bbc
bca
bcb
bcc
caa
cab
cac
cba
cbb
cbc
cca
ccb
I have tested the code for strings up to length 5 but the output is quite long so I stuck with 3 characters for this example.
CodePudding user response:
I found the answer from this docs:
https://docs.python.org/3/library/itertools.html#itertools.permutations
Itertools use this function to loop the list with permutations.
def permutations(iterable, r=None):
# permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
# permutations(range(3)) --> 012 021 102 120 201 210
pool = tuple(iterable)
n = len(pool)
r = n if r is None else r
if r > n:
return
indices = list(range(n))
cycles = list(range(n, n-r, -1))
yield tuple(pool[i] for i in indices[:r])
while n:
for i in reversed(range(r)):
cycles[i] -= 1
if cycles[i] == 0:
indices[i:] = indices[i 1:] indices[i:i 1]
cycles[i] = n - i
else:
j = cycles[i]
indices[i], indices[-j] = indices[-j], indices[i]
yield tuple(pool[i] for i in indices[:r])
break
else:
return
All together with a string:
def permutations(iterable, r=None):
# permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
# permutations(range(3)) --> 012 021 102 120 201 210
pool = tuple(iterable)
n = len(pool)
r = n if r is None else r
if r > n:
return
indices = list(range(n))
cycles = list(range(n, n-r, -1))
yield tuple(pool[i] for i in indices[:r])
while n:
for i in reversed(range(r)):
cycles[i] -= 1
if cycles[i] == 0:
indices[i:] = indices[i 1:] indices[i:i 1]
cycles[i] = n - i
else:
j = cycles[i]
indices[i], indices[-j] = indices[-j], indices[i]
yield tuple(pool[i] for i in indices[:r])
break
else:
return
def themainstring(words):
return [''.join(i) for i in permutations(words)]
thevarone = themainstring("abc");
print(thevarone)
This will print the possible variations as list. Thanks to @Henry for sharing the docs.