python - Splitting lists by short numbers -
i'm using numpy find intersections on graph, isclose returns multiple values per intersection
so, i'm going try find averages. first, want isolate similar values. useful skill feel.
i have list of x values intersection called idx looks
[-8.67735471 -8.63727455 -8.59719439 -5.5511022 -5.51102204 -5.47094188 -5.43086172 -2.4248497 -2.38476954 -2.34468938 -2.30460922 0.74148297 0.78156313 0.82164329 3.86773547 3.90781563 3.94789579 3.98797595 7.03406814 7.0741483 7.11422846] and want separate out lists each comprised of similar numbers.
this have far:
n = 0 in range(len(idx)): try: if (idx[n]-idx[n-1])<0.5: sdx.append(idx[n-1]) else: print(sdx) sdx = [] except: sdx.append(idx[n-1]) n = n+1 it works part forgets numbers:
[-8.6773547094188377, -8.6372745490981959] [-5.5511022044088181, -5.5110220440881763, -5.4709418837675354] [-2.4248496993987976, -2.3847695390781567, -2.3446893787575149] [0.7414829659318638, 0.78156312625250379] [3.8677354709418825, 3.9078156312625243, 3.9478957915831661] theres more efficient way this, know of one?
considering have numpy array, can use np.split, splitting difference > .5:
import numpy np x = np.array([-8.67735471, -8.63727455, -8.59719439, -5.5511022, -5.51102204, -5.47094188, -5.43086172, -2.4248497, -2.38476954, -2.34468938, -2.30460922, 0.74148297, 0.78156313, 0.82164329, 3.86773547, 3.90781563, 3.94789579, 3.98797595, 7.03406814, 7.0741483]) print np.split(x, np.where(np.diff(x) > .5)[0] + 1) [array([-8.67735471, -8.63727455, -8.59719439]), array([-5.5511022 , -5.51102204, -5.47094188, -5.43086172]), array([-2.4248497 , -2.38476954, -2.34468938, -2.30460922]), array([ 0.74148297, 0.78156313, 0.82164329]), array([ 3.86773547, 3.90781563, 3.94789579, 3.98797595]), array([ 7.03406814, 7.0741483 ])] np.where(np.diff(x) > .5)[0] returns index following element not meet np.diff(x) > .5) condition:
in [6]: np.where(np.diff(x) > .5)[0] out[6]: array([ 2, 6, 10, 13, 17]) + 1 adds 1 each index:
in [12]: np.where(np.diff(x) > .5)[0] + 1 out[12]: array([ 3, 7, 11, 14, 18]) then passing [ 3, 7, 11, 14, 18] np.split splits elements subarrays, x[:3], x[3:7],x[7:11] ...
Comments
Post a Comment