0

Hi all i have a list that is calculated from functions

ab = (x, x1, x2, x3, x4, x5, x6, x7, x8)

where x, x1 and so on are float numbers calculated from a distance equation. Is there any way i can take each of these float values in the list and compare them to a known value. I.e

knowndistance = 200

and if the value is greater than the known distance print greater than and if it is less than print less than. Then i would like to gather all the less than and greater than results and put them into two separte lists with one being greaterthan[] and the other lessthan[]

so i want to compare all of the values in ab to see if they are greater than is there any code that can do this for me instead of typing if for each statement. My current code is below but that doesnt work for floats?

new_list = []
for i, x in enumerate(ab):
  if x < knowndistance:
    lessthan = "Station {} average is less than {}".format(i+1, knowndistance)


comicdb = []

record = {}
record = lessthan
comicdb.append(record)
new_list.extend(comicdb)

Thanks for your help in advance :)

Alfe
  • 56,346
  • 20
  • 107
  • 159
user2423678
  • 43
  • 1
  • 8

2 Answers2

0

List comprehensions can give you a filtered set of values fast:

lessthan = [v for v in ab if v < knowndistance]
greaterthan = [v for v in ab if v >= knowndistance]

or use a loop to partition the values:

lessthan, greaterthan = [], []
for v in ab:
    if v < knowndistance:
        lessthan.append(v)
    else:
        greaterthan.append(v)

Note that greaterthan is strictly greater then or equal to here, but you can tighten up the tests if you need to ignore any values exactly equal to knowndistance.

Alfe
  • 56,346
  • 20
  • 107
  • 159
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • be careful with == checks on floats though. – wim May 30 '13 at 08:05
  • @wim: sure, floats and direct comparisons are always tricky; but for partitioning that should be fine; I don't think the OP has thought the edgecases through anyway. – Martijn Pieters May 30 '13 at 08:08
  • Thanks this seems ok but im not sure where to put it in the code. Id like to print it as well how would i do that? – user2423678 May 30 '13 at 08:08
  • @user2423678: You can loop over `lessthan` and `greaterthan` after the list comprehensions, or add `print` statements in the `if` and `else` branches of the second option. That isn't too hard to figure out, is it? – Martijn Pieters May 30 '13 at 08:10
  • @Martijn Pieters Im still rather confused as to where to put it, am i typing this over my actual code or placing it in my code. Im soo confused :( – user2423678 May 30 '13 at 08:11
  • Why don't you try things out? Take it one step at a time, use `print` a lot to see what your variables are after each step, try to work out what each line of code does. – Martijn Pieters May 30 '13 at 08:13
  • @MartijnPieters I have tried 3 different combos and all seem to produce only the last value in the original list followed by 8 [ ] im still unsure of what you are advising as i am very new to python :( – user2423678 May 30 '13 at 08:23
  • @user2423678: You are not appending values to the list, you are only printing them. Look closely at my second version; see the `.append()` calls? – Martijn Pieters May 30 '13 at 08:24
  • ahhhh i see. With being inexeprienced i did not see that. I have adjusted now so that it prints after the append statements in your second example. Thanks for pointing out that it had to be appended first. Your answers are great because i actually have to think to solve them and i feel as though its increasing my skills. Thanks @MartijnPieters – user2423678 May 30 '13 at 08:28
  • If I were to add a raw_input = knowndistance would this change the result? – user2423678 May 30 '13 at 09:33
  • No, because you are assigning to a variable named `raw_input`, shadowing the built-in function. Even `knowndistance = raw_input()` would not work, because `raw_input()` returns a string. `knowndistance = float(raw_input())` would work though. :-) – Martijn Pieters May 30 '13 at 09:36
  • ahhh thanks Martijn i have not changed the program because i dont need too. I was just trying to extend my knowledge by thinking what would happen if i were to change the result. I am experiementing as you said because i want to learn more :) You know your stuff @MartijnPieters i see why you must have so many gold badges :) – user2423678 May 30 '13 at 09:38
0

If you need to search the key often in your code, it would be advisable to pre sort the data, and perform a binary search. Based on the frequency of lookup and the length of your input data, this would be efficient

>>> import bisect
>>> import random
>>> ab = [random.random()*100 for _ in range(20)]
>>> ab = sorted(ab)
>>> knowndist = 50
>>> index = bisect.bisect_left(ab, knowndist)
>>> left = ab[:index]
>>> right = ab[index:]
>>> left
[0.7247004541477753, 4.854550484568454, 5.07097854581271, 5.768240615684894, 39.99461725759485]
>>> right
[61.05798332602631, 62.49927871117734, 64.18742114760279, 64.33592616994855, 67.47713405587675, 69.82614000336153, 70.8239641304042, 73.52120950142177, 76.36583812405388, 77.21433592853452, 80.63221055514911, 82.32348252610743, 91.75223944014984, 95.04315728608329, 99.04521000455324]
Abhijit
  • 62,056
  • 18
  • 131
  • 204
  • 1
    Based on the comments on my answer, plus the number of unused variables in the OP code, I can safely say this is **way** over the OPs experience level. – Martijn Pieters May 30 '13 at 08:15
  • @MartijnPieters: I was just reading through the comments and I seem to agree with you. But in any case, I will leave this answer for others as a reference or for OP in case he later elevates his experience with Python :-) – Abhijit May 30 '13 at 08:17