0

here is a new task: I have the following problem with choosing the data from an array

a = [1100, 1140, 1258, 1800, 2100, 2365]

I would like to choose from this arry the value that is strictly below 2000 and the one strictly above 2000... Which are in this case 1800 and 2100

Any help would be appreciated

CharlieShe
  • 63
  • 1
  • 8
  • It is not entirely clear to me what you try to achieve. Your code so far does not make sense (the `compute.CD(); a.append(CD)` part). Please try to re-formulate your question or maybe someone else can help. – Dr. Jan-Philip Gehrcke Sep 06 '12 at 15:47
  • I did simplified it, I hope it is more clear – CharlieShe Sep 06 '12 at 15:57
  • 3
    I think you are still missing some description... "strictly below" means all of the values below, but I don't think that is what you mean. I think you mean the highest value below 2000 and the lowest value of above 2000. – Andrew Jaffe Sep 06 '12 at 15:59
  • @Jan-PhilipGehrcke.... I hope u can still help me with this step a = [1100, 1140, 1258, 1800, 2100, 2365] and b = [1, 2, 3, 4, 5, 6] so Every variable from a correspond to a variable from b so using what u guys suggested if I would like to find the highest value below 2000 I use max([x for x in a if x < 2000]) but how can I print for this variable it correspondant number from b... So in this case I would endup with 1800,4 Thanks Appreciated – CharlieShe Sep 06 '12 at 16:26

4 Answers4

2

Is that what you need?

>>> a = [1100, 1140, 1258, 1800, 2100, 2365]
>>> max(x for x in a if x < 2000)
1800
>>> min(x for x in a if x > 2000)
2100

There are, fore sure, more (and maybe better) solutions.

The above solution uses so-called generator expressions (introduced in Python 2.4), i.e. this approach does not create new list objects. It just iterates through a two times. As others have pointed out, you can reduce this to a single iteration when a is already sorted.

Update (according to your comment above):

>>> from itertools import izip
>>> a = [1100, 1140, 1258, 1800, 2100, 2365]
>>> b = ['r', 's', 't', 'u', 'v', 'w']
>>> max((a,b) for a,b in izip(a,b) if a < 2000)
(1800, 'u')
>>> min((a,b) for a,b in izip(a,b) if a > 2000)
(2100, 'v')

This approach searches for the extrema in a list of 2-tuples, e.g. [(1100, 'r'), (1140, 's'), ...]. This means that for searching the extrema, values from list b are taken into consideration if values from list a are equal, cf this answer.

Just to let you know: in order to get the exact same behavior as in the first approach, i.e. really only evaluating values from a during extrema search, you can use

from operator import itemgetter
from itertools import izip
min(((a,b) for a,b in izip(a,b) if a > 2000)), key=itemgetter(0))

This uses item 0, i.e. the first items from the 2-tuples, i.e. values from list a for searching the minimum.

Community
  • 1
  • 1
Dr. Jan-Philip Gehrcke
  • 33,287
  • 14
  • 85
  • 130
2

If you were using NumPy:

a[a>2000].min()
a[a<2000].max()

NumPy arrays can be indexed by an array of booleans: you select only the elements that meet a given condition (here, whether you're above or below 2000), and the output is itself a a NumPy array (called ndarray) [Actually, what is returned is a subset of your initial array, the same elements]. As it's a ndarray, you can use its min and max methods.

You'll admit the syntax is quite readable, right?

But if you'r stuck without numpy, you could always try something like

min(i for i in a if i>2000)
max(i for i in a if i<2000)
DSM
  • 342,061
  • 65
  • 592
  • 494
Pierre GM
  • 19,809
  • 3
  • 56
  • 67
1

If you really mean the highest value below 2000 and the lowest value above 2000, then you can get them via

In [3]: max([x for x in a if x < 2000])
Out[3]: 1800


In [4]: min([x for x in a if x > 2000])
Out[4]: 2100

How does this work? The list comprehension [x for x in a if x < 2000] selects all those elements less than 2000. Then, just look for the maximum one.

If you know that the list is already sorted, there are probably more efficient ways.

Andrew Jaffe
  • 26,554
  • 4
  • 50
  • 59
0

I would just sort the array, then when you find the first value higher than 2000, take that one and the previous one.

Minion91
  • 1,911
  • 12
  • 19