Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从多维数组-列表中随机选择2列_Python_Arrays_List - Fatal编程技术网

Python 从多维数组-列表中随机选择2列

Python 从多维数组-列表中随机选择2列,python,arrays,list,Python,Arrays,List,我有一个数组列表 test_list =[[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15], [16,17,18,19,20]] 所以我想在随机选择2列时打印出所有可能的数组,例如,我可以选择第1列和第2列,也可以选择第2列和第4列,或者 a = [[1,2], [6,7], [11,12], [16,17]] b =[[2,4], [7,9], [

我有一个数组列表

test_list =[[1,2,3,4,5],
          [6,7,8,9,10],
          [11,12,13,14,15],
          [16,17,18,19,20]]
所以我想在随机选择2列时打印出所有可能的数组,例如,我可以选择第1列和第2列,也可以选择第2列和第4列,或者

a = [[1,2],
     [6,7],
     [11,12],
     [16,17]]

b =[[2,4],
    [7,9],
    [12,14],
    [17,19]]
我可以使用
my_list=[[row[0],row[1]]作为测试列表中的行]
来选择2个指定的列。但我不知道如何随机选择2列并打印出所有可能的输出。

我们可以使用它来获得特定大小的所有组合

from itertools import combinations

def transpose(mat):
    return list(zip(*mat))

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

pairs = (transpose(x) for x in combinations(transpose(test_list), 2))

for pair in pairs:
    print(pair)
给我们

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

您是尝试随机选择两列,还是列出两列的所有可能组合?无论哪种方式,您都可能希望转置矩阵,以便选择行而不是列,然后转置您的选择。如果你必须在代码中做很多这样的事情,你可能想考虑使用@ PATRIKHAUGH是的,尝试列出两个列结果的所有可能的组合,如果你想要随机列,请使用<代码>随机< /COD>包。如果需要所有组合,请使用
itertools
。通过简单的搜索即可在线获取文档。