【发布时间】:2017-06-16 16:09:09
【问题描述】:
(我知道问题的标题可能具有误导性,但我找不到任何其他方式来表述它 - 请随时编辑它)
我有两个列表,长度相同:
a = [1,2,3]
b = [4,5,6]
我想用第二个列表获取第一个列表的所有可能替换。
output[0] = [1,2,3] # no replacements
output[1] = [4,2,3] # first item was replaced
output[2] = [1,5,3] # second item was replaced
output[3] = [1,2,6] # third item was replaced
output[4] = [4,5,3] # first and second items were replaced
output[5] = [4,2,6] # first and third items were replaced
output[6] = [1,5,6] # second and third items were replaced
output[7] = [4,5,6] # all items were replaced
请注意,以下问题无法回答此问题:
- How to get all possible combinations of a list’s elements?
- combinations between two lists?
- Python merging two lists with all possible permutations
- All combinations of a list of lists
涉及先前链接的答案的可能解决方案是创建多个列表,然后对它们使用 itertools.product 方法。例如,我可以创建 3 个包含 2 个元素的列表,而不是 2 个包含 3 个元素的列表;但是,这会使代码过于复杂,如果可以的话,我宁愿避免这种情况。
有没有简单快捷的方法?
【问题讨论】:
标签: python list python-3.x