简体   繁体   English

2d numpy.array()将一个字符串与所有其他字符串进行比较,并对每个字符串重复

[英]2d numpy.array() comparing one string to all others, and repeated for each string

Supposed I have an input file with a list of strings (I'm using int's here for cleanliness), eg 1,2,3,4,5,6,...,n 假设我有一个带有字符串列表的输入文件(我在这里使用int表示整洁度),例如1,2,3,4,5,6,...,n

I would like to generate a 2D numpy.array that looks like this: 我想生成一个二维的numpy.array,看起来像这样:

a = [1,1], [1,2], [1,3], [1,4], ..., [1,n]

And then repeat with each consecutive string, eg 然后对每个连续的字符串重复,例如

a = [2,1], [2,2], [2,3], [2,4], ..., [2,n], ...[n,n]

How can I go about this? 我该怎么办?

Check out the itertools library. 查看itertools库。 The product function seems to be what you are after 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM