Using python I am trying to find all combinations of a list by changing only specific elements in it. For example, if I have a list [1,2,3,4,5,6,7,8,9] and append 3 strings to it, I would have:
[1,2,3,4,5,6,7,8,9,'string','string','string']
I would then like to find all combinations the list can take when only the position of the strings are allowed to change.
e.g
[1,2,3,4,5,'string',6,7,8,9,'string','string']
[1,2,'string',3,4,5,'string',6,7,'string',8,9]
['string',1,'string',2,3,4,'string',5,6,7,8,9]
etc, whilst still keeping the original list of numbers in the same ascending order. I am not necessarily trying to store every simple combination at once, I am just trying something like:
- Iterate through all of the possible combinations
- For each possibility, check a condition
- If true, then assign the list to a variable
- If false, then continue iterating
I have been trying to find a solution without having to use an unreasonable amount of for loops and which would be applicable to larger lists with possibly more strings appended. I have been looking at using itertools but can't seem to find an approach.
A solution I found could be to just use itertools.permutations (list with appended strings), and then use conditions to check if the numbers are in ascending order, but I was worried this approach would be really inefficient and take up heaps of memory, especially when working with larger lists.
Any help would be appreciated, thanks in advance.