[英]how to replace an element of a 2-D numpy.array with a string in python?
[英]2d numpy.array() comparing one string to all others, and repeated for each string
假设我有一个带有字符串列表的输入文件(我在这里使用int表示整洁度),例如1,2,3,4,5,6,...,n
我想生成一个二维的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)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.