在numpy中生成一个数组

时间:2013-02-11 02:31:15

标签: arrays matlab numpy

在Matlab中我可以生成这样的数组

a = [1, 3, 9:100, 201, 202];

9:100会给我9,10,...,100,所以我不必一个一个地输入。在numpy中有没有相同的方法呢?

1 个答案:

答案 0 :(得分:2)

In [3]: numpy.r_[1,3,9:100,201,202]
Out[3]: array([  1,   3,   9,  10, 11,  12,  13,  14,  15,  16,  17,  18,  19,
                20,  21,  22,  23,  24,  25,  26,  27,  28,  29,  30,  31,  32,
                33,  34,  35,  36,  37,  38,  39,  40,  41,  42,  43,  44,  45,
                46,  47,  48,  49,  50,  51,  52,  53,  54,  55,  56,  57,  58,
                59,  60,  61,  62,  63,  64,  65,  66,  67,  68,  69,  70,  71,
                72,  73,  74,  75,  76,  77,  78,  79,  80,  81,  82,  83,  84,
                85,  86,  87,  88,  89,  90,  91,  92,  93,  94,  95,  96,  97,
                98,  99, 201, 202])

请注意,切片在numpy中是右对齐的,因此9:100包括9,但不包括100.如果要包含100,请使用9:101