python - 如何将排列应用于列表?
问题描述
如何让 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']
您的示例输出似乎具有将给定排列的反向应用于列表的结果 - 如果这是您需要的输出,您需要反转最终列表或排列中的每个列表。
从这里:
排列可以“应用于”任何类似列表的对象,而不仅仅是排列。
推荐阅读
- php - 我如何将我的日期导入数据库 incule \ 和,?
- ios - 按下行时未调用表视图委托方法 (didSelectRowAt)
- bash - 获取目录中唯一文件的完整路径
- python-3.x - python 3.6.10 pip3 TLS/SSL 未配置
- javascript - 使用数组中的颜色
- reactjs - 反应:onClick event.currentTarget.textContent 返回未定义的 onClick
- azure - 以后可以将在一个 Azure 订阅中购买的证书移动到另一个 Azure 订阅吗?
- amazon-web-services - 如何使用 Aws 代码管道更新 aws 云形成堆栈
- python - 如何在多列中匹配列表
- python - QRadioButton 忽略 QLabel 几何