更优雅的方式定义numpy数组

时间:2017-03-06 18:47:47

标签: python arrays numpy

我正在寻找一种更优雅/整洁的方式来创建数字cmd.Parameters.Add(new OracleParameter("Error_Time", OracleDbType.Date)).Value = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"); 的{​​{1}}数组

我能想到的最好的是

numpy

4 个答案:

答案 0 :(得分:3)

您可以将列表理解传递给np.array,然后切掉您不需要的内容。

np.array([x*(10**y) for y in range(-1,5)  for x in range(1,10)])[:-8]

array([  1.00000000e-01,   2.00000000e-01,   3.00000000e-01,
         4.00000000e-01,   5.00000000e-01,   6.00000000e-01,
         7.00000000e-01,   8.00000000e-01,   9.00000000e-01,
         1.00000000e+00,   2.00000000e+00,   3.00000000e+00,
         4.00000000e+00,   5.00000000e+00,   6.00000000e+00,
         7.00000000e+00,   8.00000000e+00,   9.00000000e+00,
         1.00000000e+01,   2.00000000e+01,   3.00000000e+01,
         4.00000000e+01,   5.00000000e+01,   6.00000000e+01,
         7.00000000e+01,   8.00000000e+01,   9.00000000e+01,
         1.00000000e+02,   2.00000000e+02,   3.00000000e+02,
         4.00000000e+02,   5.00000000e+02,   6.00000000e+02,
         7.00000000e+02,   8.00000000e+02,   9.00000000e+02,
         1.00000000e+03,   2.00000000e+03,   3.00000000e+03,
         4.00000000e+03,   5.00000000e+03,   6.00000000e+03,
         7.00000000e+03,   8.00000000e+03,   9.00000000e+03,
         1.00000000e+04])

或者如果你想在numpy中完成所有操作,你可以使用矩阵乘法,用ravel展平,然后切掉末尾。

np.ravel(np.power(10.0, np.arange(-1,5))[:,np.newaxis]*np.arange(1,10))[:-8]

array([  1.00000000e-01,   2.00000000e-01,   3.00000000e-01,
         4.00000000e-01,   5.00000000e-01,   6.00000000e-01,
         7.00000000e-01,   8.00000000e-01,   9.00000000e-01,
         1.00000000e+00,   2.00000000e+00,   3.00000000e+00,
         4.00000000e+00,   5.00000000e+00,   6.00000000e+00,
         7.00000000e+00,   8.00000000e+00,   9.00000000e+00,
         1.00000000e+01,   2.00000000e+01,   3.00000000e+01,
         4.00000000e+01,   5.00000000e+01,   6.00000000e+01,
         7.00000000e+01,   8.00000000e+01,   9.00000000e+01,
         1.00000000e+02,   2.00000000e+02,   3.00000000e+02,
         4.00000000e+02,   5.00000000e+02,   6.00000000e+02,
         7.00000000e+02,   8.00000000e+02,   9.00000000e+02,
         1.00000000e+03,   2.00000000e+03,   3.00000000e+03,
         4.00000000e+03,   5.00000000e+03,   6.00000000e+03,
         7.00000000e+03,   8.00000000e+03,   9.00000000e+03,
         1.00000000e+04])

答案 1 :(得分:3)

np.array([x * 10**y for y in range(-1,4) for x in range(1,10)] + [1e4])

答案 2 :(得分:2)

可以使用numpy.fromfunction()

import numpy as np
a = np.fromfunction(lambda i, j: (i+1)*10**(j-1), (9, 6)).T.flatten()[:-8]

打印

[  1.00000000e-01   2.00000000e-01   3.00000000e-01   4.00000000e-01
   5.00000000e-01   6.00000000e-01   7.00000000e-01   8.00000000e-01
   9.00000000e-01   1.00000000e+00   2.00000000e+00   3.00000000e+00
   4.00000000e+00   5.00000000e+00   6.00000000e+00   7.00000000e+00
   8.00000000e+00   9.00000000e+00   1.00000000e+01   2.00000000e+01
   3.00000000e+01   4.00000000e+01   5.00000000e+01   6.00000000e+01
   7.00000000e+01   8.00000000e+01   9.00000000e+01   1.00000000e+02
   2.00000000e+02   3.00000000e+02   4.00000000e+02   5.00000000e+02
   6.00000000e+02   7.00000000e+02   8.00000000e+02   9.00000000e+02
   1.00000000e+03   2.00000000e+03   3.00000000e+03   4.00000000e+03
   5.00000000e+03   6.00000000e+03   7.00000000e+03   8.00000000e+03
   9.00000000e+03   1.00000000e+04]

优点是这是纯粹的numpy,因此与任何列表理解技术相比应该是高效的。

另一个纯粹的numpy方法是使用1e-1到1e4的对数数组的外积和1到9的线性数组。

a = np.outer(np.logspace(-1, 4,6),np.arange(1, 10)).flatten()[:-8]

答案 3 :(得分:1)

更短:

(10**(np.arange(37)//(37/4.))).cumsum()