组相似的条目python

时间:2013-02-10 20:51:23

标签: python numpy

我有一个numpy数组:

array([  2.86656000e+09,   2.86688000e+09,   2.86708000e+09,
     2.86860000e+09,   2.86884000e+09,   2.86908000e+09,
     2.86920000e+09,   2.87024000e+09,   2.87040000e+09,
     2.87056000e+09,   2.87076000e+09,   2.87108000e+09,
     2.87120000e+09,   2.87152000e+09,   2.87260000e+09,
     2.87272000e+09,   2.87280000e+09,   2.87448000e+09,
     2.87464000e+09,   2.87476000e+09,   2.87484000e+09])

对相似值进行分组的最佳方法是什么(差异不大于1000000)? 谢谢你的回答!!

1 个答案:

答案 0 :(得分:4)

作为评论中链接的解决方案的替代方案,这样的事情应该有效:

In [1]: import numpy as np

In [2]: arr = np.array([  2.86656000e+09,   2.86688000e+09,   2.86708000e+09,
   ...:      2.86860000e+09,   2.86884000e+09,   2.86908000e+09,
   ...:      2.86920000e+09,   2.87024000e+09,   2.87040000e+09,
   ...:      2.87056000e+09,   2.87076000e+09,   2.87108000e+09,
   ...:      2.87120000e+09,   2.87152000e+09,   2.87260000e+09,
   ...:      2.87272000e+09,   2.87280000e+09,   2.87448000e+09,
   ...:      2.87464000e+09,   2.87476000e+09,   2.87484000e+09])

In [3]: np.split(arr, np.where(np.diff(arr) > 1000000)[0] + 1)
Out[3]: 
[array([  2.86656000e+09,   2.86688000e+09,   2.86708000e+09]),
 array([  2.86860000e+09,   2.86884000e+09,   2.86908000e+09,
         2.86920000e+09]),
 array([  2.87024000e+09,   2.87040000e+09,   2.87056000e+09,
         2.87076000e+09,   2.87108000e+09,   2.87120000e+09,
         2.87152000e+09]),
 array([  2.87260000e+09,   2.87272000e+09,   2.87280000e+09]),
 array([  2.87448000e+09,   2.87464000e+09,   2.87476000e+09,
         2.87484000e+09])]