繁体   English   中英

如何将排列应用于列表?

[英]How can I apply a permutation to a list?

如何让 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 ,但不知道如何。

关于如何解决这个问题的任何建议?

你可以做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']

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

这里

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM