如何获得给定索引的排列?

发布于 2024-11-17 01:02:24 字数 381 浏览 6 评论 0原文

我有一个对象列表:

array = [object0,object1,object2,object3,object4]

并且我想更改给定排列的项目的顺序:

permutation = [ 2 , 4 , 0 , 1 , 3 ]

python 中是否有一个命令可以执行以下操作:

result = Permute(array,permutation)

result = [object2,object4,object0,object1,object3]

我知道我可以用一个简单的 for循环....

I've got a list of objects:

array = [object0,object1,object2,object3,object4]

and i want to change the order of the items given a permutation:

permutation = [ 2 , 4 , 0 , 1 , 3 ]

Is there a command in python that will do something like:

result = Permute(array,permutation)

result = [object2,object4,object0,object1,object3]

I know i can do it with a simple for loop....

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

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

发布评论

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

评论(5

内心荒芜 2024-11-24 01:02:24

如果我们假设 permutation0-n 的正确排列(每个排列只出现一次),那么以下代码应该有效:

result=[array[i] for i in permutation]

If we are assuming that permutation is a proper permutation of 0-n (each appears exactly once), then the following code should work:

result=[array[i] for i in permutation]
筱武穆 2024-11-24 01:02:24

在 Python 中,这可以通过列表理解轻松完成:

result = [array[i] for i in permutation]

In Python, this is easy to do with a list comprehension:

result = [array[i] for i in permutation]
半暖夏伤 2024-11-24 01:02:24

只是为了完整起见,根本没有 for 的版本:

seed = ['foo', 'bar', 'baz']
permutation = [1, 2, 0]
result = map(lambda i: seed[i], permutation)
print result # --> ['bar', 'baz', 'foo']

不过,我宁愿坚持使用列表理解。 ;)

Just for the sake of completeness a version with no for at all:

seed = ['foo', 'bar', 'baz']
permutation = [1, 2, 0]
result = map(lambda i: seed[i], permutation)
print result # --> ['bar', 'baz', 'foo']

I'd rather stick with the list comprehension guys, though. ;)

沫雨熙 2024-11-24 01:02:24

中的 shuffle 方法

import numpy as np
arr = np.arange(10)
np.random.shuffle(arr)
print(arr)

使用 numpy [1 7 5 2 9 4 3 6 0 8]

参考:
https://docs.scipy。 org/doc/numpy-1.15.0/reference/ generated/numpy.random.shuffle.html

Use shuffle method from numpy

import numpy as np
arr = np.arange(10)
np.random.shuffle(arr)
print(arr)

[1 7 5 2 9 4 3 6 0 8]

Reference:
https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.random.shuffle.html

迷爱 2024-11-24 01:02:24

您可以使用索引交换。
你有两个数组 a 和 b

def swap_random(a, b):
"""Randomly swap entries in two arrays."""
# Indices to swap
    swap_inds = np.random.random(size=len(a)) < 0.5 # your threshold 

# Make copies of arrays a and b for output
    a_out = np.copy(a)
    b_out = np.copy(b)

# Swap values
   a_out[swap_inds] = b[swap_inds]
   b_out[swap_inds] = a[swap_inds]

   return a_out, b_out

所以,做测试

d = np.array(range(0,15))
r = np.array(range(16,31))

display(d,r)

>>> array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])
>>> array([16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30])


display(swap_random(d, r))
>>> (array([ 0, 17,  2,  3, 20, 21, 22,  7, 24, 25, 10, 11, 28, 13, 14]),
>>> array([16,  1, 18, 19,  4,  5,  6, 23,  8,  9, 26, 27, 12, 29, 30]))

You can use index swapping.
You a have two array a and b

def swap_random(a, b):
"""Randomly swap entries in two arrays."""
# Indices to swap
    swap_inds = np.random.random(size=len(a)) < 0.5 # your threshold 

# Make copies of arrays a and b for output
    a_out = np.copy(a)
    b_out = np.copy(b)

# Swap values
   a_out[swap_inds] = b[swap_inds]
   b_out[swap_inds] = a[swap_inds]

   return a_out, b_out

So, do the test

d = np.array(range(0,15))
r = np.array(range(16,31))

display(d,r)

>>> array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])
>>> array([16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30])


display(swap_random(d, r))
>>> (array([ 0, 17,  2,  3, 20, 21, 22,  7, 24, 25, 10, 11, 28, 13, 14]),
>>> array([16,  1, 18, 19,  4,  5,  6, 23,  8,  9, 26, 27, 12, 29, 30]))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文