从 2 个列表生成所有交换的集合

发布于 2025-01-17 19:08:18 字数 888 浏览 3 评论 0原文

我使用Pyhton,想创建类似一组列表的东西。我解释。 作为输入,我有这样的列表:

s = [[0,4,5,6,8],[1,2,3]]

我想在所有s [0]和s [1]元素之间对此进行随机交换。问题在于我想枚举(不明确)此交换的所有可能结果。我尝试了一些Itertools的东西,但没有成功。 换句话说,预期的输出是一个可达的,包含s的所有可能交换。 欢迎任何建议!

s = [[0,4,5,6,8],[1,2,3]]

def swap(s):
    i0 = randint(0,len(s[0])-1)
    i1 = randint(0,len(s[1])-1)
    s[0][i0],s[1][i1] = s[1][i1],s[0][i0]

编辑:我认为我没有很好地解释问题。 如果我们有输入:

s = [[0,4,5,6,8],[1,2,3]]

ouput将是类似的:

S = [[[1,4,5,6,8],[0,2,3]],
     [[0,1,5,6,8],[4,2,3]],
     [[0,4,1,6,8],[5,2,3]],
     [[0,4,5,1,8],[6,2,3]],
     [[0,4,5,6,1],[8,2,3]],
     [[2,4,5,6,8],[1,0,3]],
     ...

对于s中的每个元素e,e必须与s不同,则只有一个2个元素的置换

I use pyhton and would like to create something like a set of lists. I explain.
As input, I have a list like this :

s = [[0,4,5,6,8],[1,2,3]]

I want to apply random swap on this s between all s[0] and s[1] elements. The problem is that I want to kind of enumerate (not explicitly) all the possible results of this swap. I have try some itertools stuff but didn't success.
In other words, the expected output is an iterable containing all possible swap from s.
Any advice is welcome !

s = [[0,4,5,6,8],[1,2,3]]

def swap(s):
    i0 = randint(0,len(s[0])-1)
    i1 = randint(0,len(s[1])-1)
    s[0][i0],s[1][i1] = s[1][i1],s[0][i0]

Edit : I think I did not explain well the problem.
If we have as input :

s = [[0,4,5,6,8],[1,2,3]]

The ouput would be something like :

S = [[[1,4,5,6,8],[0,2,3]],
     [[0,1,5,6,8],[4,2,3]],
     [[0,4,1,6,8],[5,2,3]],
     [[0,4,5,1,8],[6,2,3]],
     [[0,4,5,6,1],[8,2,3]],
     [[2,4,5,6,8],[1,0,3]],
     ...

For each element e in S, e must be different from s by only one permutation of 2 elements

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抚笙 2025-01-24 19:08:18

不确定我是否正确地解决了您的问题,但是您可以使用 itertools.combinations 用于生成所有可能的掉期

from itertools import combinations

listIn = [[0,4,5,6,8],[1,2,3]]

shortList, longList = sorted(listIn, key=len)

result = []

for longPart in combinations(longList,len(longList)-1):
    for shortPart in combinations(shortList,len(shortList)-2):
        longPart = list(longPart)
        shortPart = list(shortPart)
        p1 = longPart + shortPart

        p2 = list(set(longList) - set(longPart)) + list(set(shortList) - set(shortPart))
        result.append([p1, p2])

对于您的示例,结果列表包含所有15个可能的单swaps。

这里的摘录:

[[0, 4, 5, 6, 1], [8, 2, 3]]
[[0, 4, 5, 6, 2], [8, 1, 3]]
[[0, 4, 5, 6, 3], [8, 1, 2]]
[[0, 4, 5, 8, 1], [6, 2, 3]]
[[0, 4, 5, 8, 2], [6, 1, 3]]
[[0, 4, 5, 8, 3], [6, 1, 2]]
[[0, 4, 6, 8, 1], [5, 2, 3]]
[[0, 4, 6, 8, 2], [5, 1, 3]]
[[0, 4, 6, 8, 3], [5, 1, 2]]
[[0, 5, 6, 8, 1], [4, 2, 3]]
[[0, 5, 6, 8, 2], [4, 1, 3]]
[[0, 5, 6, 8, 3], [4, 1, 2]]
[[4, 5, 6, 8, 1], [0, 2, 3]]
[[4, 5, 6, 8, 2], [0, 1, 3]]
[[4, 5, 6, 8, 3], [0, 1, 2]]

Not sure if I get your question right, but you can use itertools.combinations for generating all possible swaps.

from itertools import combinations

listIn = [[0,4,5,6,8],[1,2,3]]

shortList, longList = sorted(listIn, key=len)

result = []

for longPart in combinations(longList,len(longList)-1):
    for shortPart in combinations(shortList,len(shortList)-2):
        longPart = list(longPart)
        shortPart = list(shortPart)
        p1 = longPart + shortPart

        p2 = list(set(longList) - set(longPart)) + list(set(shortList) - set(shortPart))
        result.append([p1, p2])

For your example the result list contains all 15 possible single-swaps.

Here an extract:

[[0, 4, 5, 6, 1], [8, 2, 3]]
[[0, 4, 5, 6, 2], [8, 1, 3]]
[[0, 4, 5, 6, 3], [8, 1, 2]]
[[0, 4, 5, 8, 1], [6, 2, 3]]
[[0, 4, 5, 8, 2], [6, 1, 3]]
[[0, 4, 5, 8, 3], [6, 1, 2]]
[[0, 4, 6, 8, 1], [5, 2, 3]]
[[0, 4, 6, 8, 2], [5, 1, 3]]
[[0, 4, 6, 8, 3], [5, 1, 2]]
[[0, 5, 6, 8, 1], [4, 2, 3]]
[[0, 5, 6, 8, 2], [4, 1, 3]]
[[0, 5, 6, 8, 3], [4, 1, 2]]
[[4, 5, 6, 8, 1], [0, 2, 3]]
[[4, 5, 6, 8, 2], [0, 1, 3]]
[[4, 5, 6, 8, 3], [0, 1, 2]]
七分※倦醒 2025-01-24 19:08:18

也许我误解了这个问题,但是如果您希望所有组合将列表1的元素与列表2的元素交换为2。我们创建了嵌套列表的副本并在这两个列表中的份量中删除元素,这是另一种方法

s = [[0, 4, 5, 6, 8], [1, 2, 3]]

output = []
for i in range(len(s[0])):
    for j in range(len(s[1])):
        temp_s = [s[0].copy(), s[1].copy()]
        temp_s[0][i], temp_s[1][j] = temp_s[1][j], temp_s[0][i]
        output.append(temp_s)

输出:

[[[1, 4, 5, 6, 8], [0, 2, 3]],
 [[2, 4, 5, 6, 8], [1, 0, 3]],
 [[3, 4, 5, 6, 8], [1, 2, 0]],
 [[0, 1, 5, 6, 8], [4, 2, 3]],
 [[0, 2, 5, 6, 8], [1, 4, 3]],
 [[0, 3, 5, 6, 8], [1, 2, 4]],
 [[0, 4, 1, 6, 8], [5, 2, 3]],
 [[0, 4, 2, 6, 8], [1, 5, 3]],
 [[0, 4, 3, 6, 8], [1, 2, 5]],
 [[0, 4, 5, 1, 8], [6, 2, 3]],
 [[0, 4, 5, 2, 8], [1, 6, 3]],
 [[0, 4, 5, 3, 8], [1, 2, 6]],
 [[0, 4, 5, 6, 1], [8, 2, 3]],
 [[0, 4, 5, 6, 2], [1, 8, 3]],
 [[0, 4, 5, 6, 3], [1, 2, 8]]]

Maybe I misunderstood the question, but here is another way to do so if you want all combinations swapping an element of list 1 with an element of list 2.

We create a copy of the nested list and permute elements in those two lists.

s = [[0, 4, 5, 6, 8], [1, 2, 3]]

output = []
for i in range(len(s[0])):
    for j in range(len(s[1])):
        temp_s = [s[0].copy(), s[1].copy()]
        temp_s[0][i], temp_s[1][j] = temp_s[1][j], temp_s[0][i]
        output.append(temp_s)

Output:

[[[1, 4, 5, 6, 8], [0, 2, 3]],
 [[2, 4, 5, 6, 8], [1, 0, 3]],
 [[3, 4, 5, 6, 8], [1, 2, 0]],
 [[0, 1, 5, 6, 8], [4, 2, 3]],
 [[0, 2, 5, 6, 8], [1, 4, 3]],
 [[0, 3, 5, 6, 8], [1, 2, 4]],
 [[0, 4, 1, 6, 8], [5, 2, 3]],
 [[0, 4, 2, 6, 8], [1, 5, 3]],
 [[0, 4, 3, 6, 8], [1, 2, 5]],
 [[0, 4, 5, 1, 8], [6, 2, 3]],
 [[0, 4, 5, 2, 8], [1, 6, 3]],
 [[0, 4, 5, 3, 8], [1, 2, 6]],
 [[0, 4, 5, 6, 1], [8, 2, 3]],
 [[0, 4, 5, 6, 2], [1, 8, 3]],
 [[0, 4, 5, 6, 3], [1, 2, 8]]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文