假设我有一个带有字符串列表的输入文件(我在这里使用 int's 是为了清洁),例如 1,2,3,4,5,6,...,n
我想生成一个如下所示的 2D numpy.array:
a = [1,1], [1,2], [1,3], [1,4], ..., [1,n]
然后对每个连续的字符串重复,例如
a = [2,1], [2,2], [2,3], [2,4], ..., [2,n], ...[n,n]
我该怎么办?
查看itertools库。该product
功能似乎是您所追求的
In [19]: list( product([1, 2, 3, 4, 5], repeat=2) )
Out[19]:
[(1, 1),
(1, 2),
(1, 3),
(1, 4),
(1, 5),
(2, 1),
(2, 2),
(2, 3),
(2, 4),
(2, 5),
(3, 1),
(3, 2),
(3, 3),
(3, 4),
(3, 5),
(4, 1),
(4, 2),
(4, 3),
(4, 4),
(4, 5),
(5, 1),
(5, 2),
(5, 3),
(5, 4),
(5, 5)]