4

I have the list

\[ScriptCapitalD] = 
  {824, 1082, 1135, 1308, 1359, 1372, 1401, 1412, 1601, 
   1638, 1641, 1674, 1709, 1805, 1947, 2208}

I would like to extract a sublist according to certain defined conditions, such as extracting only values which lie in the range 1000-1500.

What is the best way to do this?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Physkid
  • 680
  • 5
  • 13

4 Answers4

6

The standard Mathematica function for that kind of selection, is Select.

data = 
  {824, 1082, 1135, 1308, 1359, 1372, 1401, 1412, 1601, 1638, 1641, 1674, 1709, 
   1805, 1947, 2208};
Select[data, Between[#, {1000, 1500}] &]

{1082, 1135, 1308, 1359, 1372, 1401, 1412}

Update

Between has an operator form, so

Select[data, Between[{1000, 1500}]]

will work and is nicer.

Select has its operator form, too, so one can also write

Select[Between[{1000, 1500}]] @ data
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
4
sdt = Select[dt, 1000 <= # <= 1500 &]

{1082, 1135, 1308, 1359, 1372, 1401, 1412}
Anxon Pués
  • 907
  • 5
  • 13
1
Cases[Clip[lst, {1000, 1500}, {0, 0}], Except[0]]

{1082, 1135, 1308, 1359, 1372, 1401, 1412}

Alternatives, based on Clip:

lst // Pick[#, Clip[#, {1000, 1500}, {0, 0}] // Unitize, 1] &
Clip[lst, {1000, 1500}, {0, 0}] /. 0 -> Nothing
user1066
  • 17,923
  • 3
  • 31
  • 49
0

If you selection criteria is always a window (Between[{a,b}]), sorting the data and then doing a binary search may be more efficient. See Windowed binary search

John McGee
  • 411
  • 2
  • 4