Python 生成未知多维度的index
Python 生成未知维度的index, ref:Python:列出列表中所有元素的组合可能
from itertools import combinations
def combine(temp_list, n):
'''根据n获得列表中的所有可能组合(n个元素为一组)'''
temp_list2 = []
for c in combinations(temp_list, n):
temp_list2.append(c)
return temp_list2
order_dims2 = [2,3,4,5,6] # [3,4,5]
list1 = []
for i in order_dims2:
list1.extend(range(i))
end_list = []
for i in range(len(list1)):
end_list.extend(combine(list1, i))
# print(end_list)
end_list = set([i_ for i_ in end_list if (len(i_) == len(order_dims2))])
# 过滤
for loc in range(int(len(order_dims2)/2 +1)):
i = loc
j = len(order_dims2) - i - 1
if j - i >= 2:
end_list = set([i_ for i_ in end_list if (i_[i] < order_dims2[i]) and (i_[j] < order_dims2[j])])
else:
end_list = set([i_ for i_ in end_list if (i_[i] < order_dims2[i])])
# 排序
for loc in range(len(order_dims2)-1, -1, -1):
print(loc)
end_list = sorted(end_list, key=lambda x: x[loc])
reshape order comment
# ------------------------------------------------------------------------------------------------------
# reshape order:
# ref:https://blog.csdn.net/qq_38604355/article/details/112761457
# -----------------------------------------------------------------------------------------------------
# # - order = 'C', 先行后列,【dim1, dim2, ... dimN-1 dimN】, 我们都可以得到最后两维度形成的array,
# 初始值,【0, 0, ... ,0, dimN-1 dimN】,对二维矩阵按照行进行flatten,
# Next 【0, 0, ... ,1, dimN-1 dimN】,对二维矩阵按照行进行flatten按照行进行
# ...
# End: 【dim1, dim2, ... ,dimN-1, dimN-1 dimN】,对二维矩阵按照行进行flatten按照行进行
# # - order = 'F', 先列后行 【dim1, dim2, ... dimN-1 dimN】, 我们都可以得到最后两维度形成的array,
# 对最后两维的每个元素位置进行遍历,从最后两维的 【0,0】开始,然后是【1,0】,...
# 初始值:【0, 0, 0... ,0, 0 (dimN-2 loc), 0, 0】,
# 【0, 1, 0... ,0, 0 (dimN-2 loc), 0, 0】,
# ...,
# 【0, dim2, 0... ,0 , 0 (dimN-2 loc), 0, 0】
# 按dim2进行flatten ,
# 【0, 0, 1, ... ,0, 0 (dimN-2 loc), 0, 0】,
# 【0, 1, 1,... ,1, 0 (dimN-2 loc), 0, 0】,
# ...,
# 【0, dim2,1, ... ,dimN-3 , 1 (dimN-2 loc), 0, 0】
# 按dim2进行flatten ,依次循环到dim3结束
# Next: 向以后的维度进行拓展
# 【0, 0, 0, 1, ... ,0, 0 (dimN-2 loc), 0, 0】,
# 【0, 1, 0, 1,... ,1, 0 (dimN-2 loc), 0, 0】,
# ...,
# 【0, dim2,0, 1, ... ,0 , 0 (dimN-2 loc), 0, 0】
# 按dim2进行flatten ,依次循环到dim3结束
# -----------------------------------------------------------------------------------------------------
# reshape成 1000 * (3*4*5) 的二维,
# 3*4*5的顺序确定,,
# - order = 'C', 先行后列,偏历完每个channel后在偏历下一个channel,每个channel按照 4*5 的行距 flatten,
# 最终某一行【channel[0][0][0],channel[0][0][1],...,channel[2]][3][4]】
# - order = 'A', 就是根据内存里的数据读,是什么顺序就什么顺序(C或F),然后然后以同样的顺序,填充到新的shape的矩阵里
# 最终某一行【channel[0][0][0],channel[0][0][1],...,channel[2]][3][4]】
# - order = 'F', 先列后行,对每个3个channel中的对应元素,在按照每个channel中的列进行flatten,
# 最终某一行【channel[0][0][0],channel[1][0][0],channel[2][0][0], channel[0][1][0], ...,channel[1]][0][0]】