我正在尝试对图像阵列中的对进行混洗,这对我来说有点棘手。
示例: 我有20个图像序列,每个图像序列包含1000帧..它们保存在一个数组中..所以让我们假设,数组看起来像这样
[[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帧..但是我最后要实现的是对连续帧进行混洗,所以类似
[[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] ....]
类似这样的事情..所以我想产生一个洗牌,但不是单个元素,而是每个元素以及后面的一个元素,即我想洗牌对。
有办法吗?
答案 0 :(得分:2)
我正在处理一个更干净的嵌套列表(不信任[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']]
然后我创建一个具有理解力的嵌套列表,即成对列表:
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个重叠对(每个条目一对,最后一个除外)。
请注意,在下面的所有输出中,我都手动添加了换行符以提高可读性 和清晰的目的。
此时的输出是:
版本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内的对,而不洗洗对内的,这是首先制作对的列表的要点。
import random
random.shuffle(S)
S
这时的输出,当然仍然嵌套:
不重叠的随机对:
[[['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']]]
也许您可以将其用于项目的其余部分。
如果不能解散,那么
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']]
答案 1 :(得分:0)
我们将需要一些导入来简化操作:
from functools import reduce
from itertools import groupby
import operator
import random
假设您有一个类似的列表:
ar = [['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']]
我们可以先按图像对它们进行分组,以便仅生成有效对:
groupedbyimage = [list(g[1]) for g in groupby(ar, lambda x: x[0][0])]
在您的情况下,上述lambda必须是其他内容,以便在不只是比较嵌套列表中字符串中的第一个字母时生成正确的组。
接下来,我们可以将图像列表压缩一个,并移动一个:
groupedandpaired = [list(zip(x, x[1:])) for x in groupedbyimage]
我们现在可以将groupedandpaired
列表展平为pairlist
:
pairlist = reduce(operator.concat, groupedandpaired)
现在,我们可以对列表进行混洗,然后再次对其进行展平以获得所需的结果:
random.shuffle(pairlist)
result = list(reduce(operator.concat, pairlist))
然后在REPL中打印结果,我们将看到类似以下内容的
:>>> result
[['1_5'], ['1_6'], ['2_1'], ['2_2'], ['1_1'], ['1_2'], ['3_2'],
['3_3'], ['2_2'], ['2_3'], ['3_5'], ['3_6'], ['3_1'], ['3_2'],
['1_3'], ['1_4'], ['1_4'], ['1_5'], ['3_4'], ['3_5'], ['3_3'],
['3_4'], ['2_5'], ['2_6'], ['2_3'], ['2_4'], ['1_2'], ['1_3'],
['2_4'], ['2_5']]