如何改组隐式对数组? [英] How to shuffle array of implicit pairs?
问题描述
我正在尝试对图像阵列中的对进行混洗,这对我来说有点棘手.
I am trying to do shuffling of pairs in an array of images, and it is a little bit tricky for me.
示例:我有20个图像序列,每个图像序列包含1000帧..它们保存在一个数组中..所以让我们假设,该数组看起来像这样
Example: I have 20 sequences of images, each containing 1000 frames .. they are saved in an array .. so let's assume, the array looks like that
[[1_1],[1_2],[1_3],[1_4],[1_5],[1_6],[2_1],[2_2],[2_3],[2_4],[2_5],[2_6],[3_1],[3_2],[3_3],[3_4],[3_5],[3_6]]
以此类推,这只是3个序列的最小示例,每个序列有6帧..但最后我要实现的是对连续帧进行混洗,所以类似
And so on, this is just a minimal example of 3 sequences, each having 6 frames .. but what I want to achieve in the end, of shuffling of sequential frames, so something like that
[[1_4],[1_5],[2_3],[2_4],[3_5],[3_6],[1_3],[1_4],[1_1],[1_2],[3_2],[3_3],[1_2],[1_3],[3_3],[3_4],[2_1],[2_2] ....]
类似这样的事情..所以我想产生一个洗牌,但不是单个元素,而是每个元素以及后面的一个元素,即我想洗牌对.
So something like this simply .. So I want to produce a shuffle, but instead of single elements, it will be each element along with the follwoing one, i.e. I want to shuffle pairs.
有办法吗?
推荐答案
我正在研究一个更干净的嵌套列表(不信任 [1_1]
):
I am working on a cleaner nested list (mistrusting the [1_1]
):
L = [['1_1'],['1_2'],['1_3'],['1_4'],['1_5'],['1_6'],['2_1'],['2_2'],['2_3'],['2_4'],['2_5'],['2_6'],['3_1'],['3_2'],['3_3'],['3_4'],['3_5'],['3_6']]
然后我创建一个具有理解力的嵌套列表,即成对列表:
Then I create a nested list, i.e. a list of pairs, with a comprehension:
S=[[L[z*2+y] for y in range(2)] for z in range(len(L)//2)] # version 1
S=[[L[z+y] for y in range(2)] for z in range(len(L)-1)] # version 2
S
请注意,有一对长度为2的非重叠对,但是N-1个重叠对(每个条目一对,最后一个除外).
Note that there are length/2 non-overlapping pairs, but N-1 overlapping pairs (one pair for each entry, except the last).
请注意,在下面的所有输出中,我都手动添加了换行符以提高可读性和清晰的目的.
Note that in all outputs below I have manually added newlines for readability and clarity purposes.
此时的输出是:
版本1,非重叠对:
[[['1_1'], ['1_2']],
[['1_3'], ['1_4']],
[['1_5'], ['1_6']],
[['2_1'], ['2_2']],
[['2_3'], ['2_4']],
[['2_5'], ['2_6']],
[['3_1'], ['3_2']],
[['3_3'], ['3_4']],
[['3_5'], ['3_6']]]
版本2,重叠对:
[[['1_1'], ['1_2']],
[['1_2'], ['1_3']],
[['1_3'], ['1_4']],
[['1_4'], ['1_5']],
[['1_5'], ['1_6']],
[['1_6'], ['2_1']],
[['2_1'], ['2_2']],
[['2_2'], ['2_3']],
[['2_3'], ['2_4']],
[['2_4'], ['2_5']],
[['2_5'], ['2_6']],
[['2_6'], ['3_1']],
[['3_1'], ['3_2']],
[['3_2'], ['3_3']],
[['3_3'], ['3_4']],
[['3_4'], ['3_5']],
[['3_5'], ['3_6']]]
然后随机播放S,它将仅对S内的对进行随机播放,而不对对内的进行随机播放,这是首先对对列表进行排序的原因.
Then shuffle S, which will only shuffle the pairs within S, not within the pairs, that is the point of making the list of pairs first.
import random
random.shuffle(S)
S
这时的输出,当然仍然是嵌套的:
Output at this point, still nested of course:
不重叠的随机对:
[[['3_3'], ['3_4']],
[['3_1'], ['3_2']],
[['2_3'], ['2_4']],
[['3_5'], ['3_6']],
[['1_1'], ['1_2']],
[['1_3'], ['1_4']],
[['2_1'], ['2_2']],
[['1_5'], ['1_6']],
[['2_5'], ['2_6']]]
输出重叠的随机对:
[[['1_2'], ['1_3']],
[['2_1'], ['2_2']],
[['2_4'], ['2_5']],
[['2_2'], ['2_3']],
[['1_3'], ['1_4']],
[['3_4'], ['3_5']],
[['3_3'], ['3_4']],
[['3_2'], ['3_3']],
[['1_6'], ['2_1']],
[['2_5'], ['2_6']],
[['2_6'], ['3_1']],
[['1_4'], ['1_5']],
[['1_1'], ['1_2']],
[['2_3'], ['2_4']],
[['1_5'], ['1_6']],
[['3_1'], ['3_2']],
[['3_5'], ['3_6']]]
也许您可以将其用于项目的其余部分.
如果不能解散,那么
Maybe you can use that for the rest of the project.
If not dissolve the pairs
L2=[]
for x in S:
for y in x:
L2.append(y)
print(L2)
输出溶解的非重叠对:
[['3_3'], ['3_4'], ['3_1'], ['3_2'], ['2_3'], ['2_4'],
['3_5'], ['3_6'], ['1_1'], ['1_2'], ['1_3'], ['1_4'],
['2_1'], ['2_2'], ['1_5'], ['1_6'], ['2_5'], ['2_6']]
输出溶解的重叠对:
[['1_2'], ['1_3'], ['2_1'], ['2_2'], ['2_4'], ['2_5'],
['2_2'], ['2_3'], ['1_3'], ['1_4'], ['3_4'], ['3_5'],
['3_3'], ['3_4'], ['3_2'], ['3_3'], ['1_6'], ['2_1'],
['2_5'], ['2_6'], ['2_6'], ['3_1'], ['1_4'], ['1_5'],
['1_1'], ['1_2'], ['2_3'], ['2_4'], ['1_5'], ['1_6'],
['3_1'], ['3_2'], ['3_5'], ['3_6']]
这篇关于如何改组隐式对数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!