0

I have two lists in Python that look like this:

 lst = [1, '?2']
 replace_lst1 = ['a','b','c']

For each occurrence of '?2' in lst, I would like to replace it with each element from replace_lst1 thereby producing a list of lists as follows:

res = [ [1,'a'], 
        [1,'b'],
        [1,'c'] ]

Similarly, if I have the following lists:

lst = [1, '?2','?3']
replace_lst1 = ['a','b','c']
replace_lst2 = ['A', 'B', 'C']

I would like to replace '?2' with each element from replace_lst1 and '?3' with each element from replace_lst2, thereby exploring all possible permutations. The result should look like this:

res = [ [1,'a','A'], 
        [1,'a','B'],
        [1,'a','C'],
        [1,'b','A'], 
        [1,'b','B'],
        [1,'b','C'],
        [1,'c','A'], 
        [1,'c','B'],
        [1,'c','C'] ]

It would be great if you could provide me with some suggestions how to proceed.

Thanks!

5
  • what is the purpose of this? Do you ever have replace_lst3? Commented Jun 6, 2013 at 11:58
  • are the ?N format mandatory? shall it begin at 1? it's definitively possible to do what you want, but it looks pretty weird and won't be elegant... Commented Jun 6, 2013 at 12:01
  • 3
    Unless this is being imposed on you from some external place, I would strongly advise you to use a different data model. What you want can be done very elegantly with a single line of code, but not if you define your data like this. Commented Jun 6, 2013 at 12:01
  • 1
    In this context, permutations has a specific meaning, and it isn't what you want Commented Jun 6, 2013 at 12:17
  • There is no replace_lst3. The ?X format is given. It doesn't begin at 1, actually, the lists are strings. I simplified the things for illustrative purposes. As for the data structure, I'd like the result to be a list of lists since I'll have to do some operations on the result and with this data structure, I know how to proceed. Commented Jun 6, 2013 at 12:19

3 Answers 3

3

If you change your data structure slightly, this is a trivial problem for the itertools module:

>>> lst = [1]
>>> combine = [["a", "b", "c"], ["A", "B", "C"]]
>>> import itertools
>>> [lst+list(item) for item in itertools.product(*combine)]
[[1, 'a', 'A'], [1, 'a', 'B'], [1, 'a', 'C'], [1, 'b', 'A'], [1, 'b', 'B'], 
 [1, 'b', 'C'], [1, 'c', 'A'], [1, 'c', 'B'], [1, 'c', 'C']]
Sign up to request clarification or add additional context in comments.

Comments

3
>>> from itertools import product
>>> lst = [1]
>>> combine = [["a", "b", "c"], ["A", "B", "C"]]
>>> list(product(*[lst]+combine))
[(1, 'a', 'A'), (1, 'a', 'B'), (1, 'a', 'C'), (1, 'b', 'A'), (1, 'b', 'B'), (1, 'b', 'C'), (1, 'c', 'A'), (1, 'c', 'B'), (1, 'c', 'C')]

you could also use

list(product(lst, replace_lst1, replace_lst2))

Comments

0

I would use itertools.product, plus a test to replace the ? by the value :

lst = [1, '?2','?3']
replace_lst1 = ['a','b','c']
replace_lst2 = ['A', 'B', 'C']
res = []
#put as many replace_lst as you need here
for values in itertools.product(replace_lst1, replace_lst2):
    val_iter = iter(values)
    res.append([x if str(x).find('?') == -1 else next(val_iter) for x in lst])

The use of val_iter allows for the ?* to be placed anywhere (but not in any order, though).

1 Comment

Thank you all for the responses! itertools.product is exactly what I needed! Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.