0

I am trying to extract the data from 15020 to -15431. Is it possible for me to extract such a range? The x value is often different as it is read by a file.

Range
x = 15021,14999,14888,...,0,-1000,-14000,-15431,-14000,-2000,0,1000,7000,15890
  # from here to---------------------------here

Here, 15021 is the first value.

-15431 is the minimum value. minimum(x) = -15431

How can I extract data from the first value to the min value?

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
  • Depends. If the array never changes, then you can use [array slicing](http://stackoverflow.com/questions/509211/pythons-slice-notation) with hardcoded indexes, otherwise you'll need to search the array. – Chris Laplante Nov 04 '13 at 01:25
  • 1
    What are you *really* trying to do? Are you, perhaps, trying to find the longest initial sequence that's strictly decreasing? – Tim Peters Nov 04 '13 at 01:31
  • Is that meant to represent the contents of your file or is it meant to be code? – YXD Nov 04 '13 at 01:32
  • I am trying to plot a interpolation graph but for interp1d it only take from postive to 0 to negative anything else is a no. thus i have to know how to extract out positive to negative value @TimPeters –  Nov 04 '13 at 01:33
  • @ZhenHui, but the example you gave includes 3 negative values, not 1. And then it does *not* include two more negative values following -15431. It's very unclear why you stopped at -15431. Why didn't you stop at -1000? At -14000? At -2000? What's special about -15431? – Tim Peters Nov 04 '13 at 01:35
  • Sorry i might not explained clearly.. As -15431 is like the lowest range for negative value. maybe i can get min(x) to obtain the negative value @TimPeters –  Nov 04 '13 at 01:37
  • Then read my first question again ;-) It sure sounds like *are* you trying to find the longest initial sequence that's strictly decreasing (or maybe just non-increasing). Your main problem here is that you really haven't defined what you need :-) – Tim Peters Nov 04 '13 at 01:39

1 Answers1

3

Assuming x is a list, the slice from the first value to the smallest value can be written

x[:x.index(min(x)) + 1]

Like so:

>>> x = [15021,14999,14888,0,-1000,-14000,-15431,-14000,-2000,0,1000,7000,15890]
>>> x[:x.index(min(x)) + 1]
[15021, 14999, 14888, 0, -1000, -14000, -15431]
Tim Peters
  • 67,464
  • 13
  • 126
  • 132