给定 2 个包含数字 0 到 (n-1) 的列表 (list_1, list_2),如何获得排列 p(S(n) 的元素)使得 p(list_1) = list_2?

发布于 2025-01-11 17:03:04 字数 434 浏览 0 评论 0原文

我一直在使用 sympy 排列包。到目前为止,我已声明排列如下

from sympy.combinatorics.generators import symmetric, Permutation
p = Permutation([[2, 3], [5]])
print(p(([0, 1, 2, 3, 4, 5]))

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

我想声明给定 2 个列表的排列。例如,我希望元素

排列作用于整数而不是列表中的位置(这样 (01) * [1, 2, 3, 0] = [0, 2, 3, 1],而不是 (01) * [1, 2, 3, 0] = [2, 1, 3, 0])

我该怎么做?

I have been using the sympy permutations package. So far I have declared permutations as follows

from sympy.combinatorics.generators import symmetric, Permutation
p = Permutation([[2, 3], [5]])
print(p(([0, 1, 2, 3, 4, 5]))

out:[0, 1, 3, 2, 4, 5]

I would like to declare a permutation given 2 lists. For example, I would like the element

I would like the permutation to act on integers rather than positions in the list (so that (01) * [1, 2, 3, 0] = [0, 2, 3, 1], instead of (01) * [1, 2, 3, 0] = [2, 1, 3, 0])

How can I do this ?

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

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

发布评论

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

评论(1

猫性小仙女 2025-01-18 17:03:04

请参阅 permutations.py 中有关组合的讨论:

>>> start = Permutation([1, 0, 2, 3])
>>> finish = Permutation([2, 3, 1, 0])
>>> p = finish*start
>>> p([1,0,2,3])
[2, 3, 1, 0]
>>> p
Permutation(0, 2)(1, 3)

也许是更好的方法,但如果您想按名称而不是位置访问,这似乎可行:

from sympy.combinatorics import Permutation
from sympy.utilities.iterables import permutations
P = Permutation
start = [1,0,2,3]
end = [2,3,1,0]
do = P(end)*P(start)
def byname(l):
    a = do([l.index(i) for i in range(len(l))])
    return [a.index(i) for i in range(len(l))]

for i in permutations(range(4)):
  i, byname(i)

See the discussion about composition in permutations.py:

>>> start = Permutation([1, 0, 2, 3])
>>> finish = Permutation([2, 3, 1, 0])
>>> p = finish*start
>>> p([1,0,2,3])
[2, 3, 1, 0]
>>> p
Permutation(0, 2)(1, 3)

Maybe a better way to do this, but this seems to work if you want to access by name instead of position:

from sympy.combinatorics import Permutation
from sympy.utilities.iterables import permutations
P = Permutation
start = [1,0,2,3]
end = [2,3,1,0]
do = P(end)*P(start)
def byname(l):
    a = do([l.index(i) for i in range(len(l))])
    return [a.index(i) for i in range(len(l))]

for i in permutations(range(4)):
  i, byname(i)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文