1

Consider the lists below

filters= [u,    i,  g,    r,    z]
result = [None, 34, None, None, 45]

the items in the result are computed for each filter in filters. It happens that filters, u, g, z did not return any results. So i would to re-compute the values of result[0], result[2], result[4], using the filters that returned values.

My problem is iterating through both lists and using the closet filter to compute a value missing in result. e.g result[0] should be computed using 'i' (i is closest to u) result[2] we also use 'i' not 'z' and result[3] we use 'z'. How to generalize this?? (filters are fixed, but items in values keep changing.) What i would like to get is a tuple with two filters, (filter_missing_a_value_in_results, filter_to_used_to_computer_the_missing_value)

4
  • What would happen with result[2] if result[3] were populated? Commented May 5, 2011 at 11:57
  • Any closest filter can be used the one before or after Commented May 5, 2011 at 12:00
  • 3
    You can't have "before or after". You need to define your behavior. Commented May 5, 2011 at 12:01
  • After is the best option- but if last value is not known, then a before can be used Commented May 5, 2011 at 12:05

1 Answer 1

1

Not particularly efficient solution:

def filters_by_distance(i):
    '''Generate filters by distance from i'''
    sorted_indices = sorted(range(len(filters)), key=lambda j: abs(j-i))
    return (filters[j] for j in sorted_indices)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.