【问题标题】:How can I apply a permutation to a list?如何将排列应用于列表?
【发布时间】:2019-12-13 16:36:51
【问题描述】:

如何让 Sympy Permutation 作用于列表?例如,

from sympy.combinatorics import Permutation

lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
perm = Permutation([[0, 2, 8, 6], [1, 5, 7, 3]])
# Then something like...
perm * lst   # This doesn't work. Throws AttributeError because of list

我想要这样的返回值(在本例中):

['g', 'd', 'a', 'h', 'e', 'b', 'i', 'f', 'c']

我已阅读 https://docs.sympy.org/latest/modules/combinatorics/permutations.html,但不知道如何阅读。

有什么建议可以解决这个问题吗?

【问题讨论】:

    标签: python python-3.x list permutation sympy


    【解决方案1】:

    你可以perm(lst)

    >>> from sympy.combinatorics import Permutation
    >>> lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
    >>> perm = Permutation([[0, 2, 8, 6], [1, 5, 7, 3]])
    >>> perm(lst)
    ['c', 'f', 'i', 'b', 'e', 'h', 'a', 'd', 'g']
    

    您的示例输出似乎具有将给定排列的反向应用于列表的结果 - 如果这是您需要的输出,您需要反转最终列表或排列中的每个列表。

    来自here

    排列可以“应用于”任何类似列表的对象,而不仅仅是排列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-29
      • 1970-01-01
      • 2011-03-03
      • 1970-01-01
      • 2018-11-24
      • 2011-08-31
      • 2023-04-06
      • 2021-07-28
      相关资源
      最近更新 更多