![]() |
getting previous/next valid value in a list, cyclically - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: getting previous/next valid value in a list, cyclically (/thread-9064.html) |
getting previous/next valid value in a list, cyclically - studeo - Mar-19-2018 I have a list of values... some are valid, some invalid (containing None). I test each element for validity. When I find an invalid element, I need to find indexes of the closest previous and the closest next valid value. I need to treat the list as a cycle, so if there are no valid elements before/after the initial element, it should search from the end/start of the list... This way, if there is at least one valid value in the list, both the previous and the next valid value will be found (it can be the same index, if there is only one valid value in the list). I am able to solve it, but with a ridiculously complicated code. Could you help me find some elegant solution? I am not a programmer, just using my very humble programming skills to process data in another field. RE: getting previous/next valid value in a list, cyclically - micseydel - Mar-19-2018 Can you post what you've tried, and some sample uses of the code? RE: getting previous/next valid value in a list, cyclically - studeo - Mar-19-2018 example_values = [None, None, 2, None, 4, None, None, 7, None, None] for i, v in enumerate(example_values): if v is not None: print("value at position {} is valid: {}".format(i,v)) else: print("value at position {} is invalid".format(i)) prev_valid = False next_valid = False for j, w in enumerate(example_values): if j < i and w is not None: prev_valid = j if not prev_valid: for j, w in enumerate(example_values): if w is not None: prev_valid = j for j, w in enumerate(example_values): if j > i and w is not None: next_valid = j break if not next_valid: for j, w in enumerate(example_values): if w is not None: next_valid = j break print(" previous valid value is at position {}".format(prev_valid)) print(" next valid value is at position {}".format(next_valid)) RE: getting previous/next valid value in a list, cyclically - micseydel - Mar-19-2018 Can you give a very, very simplified example of what you want? It seems extremely complex right now, and I'm not sure if it's because of the requirements or the implementation. RE: getting previous/next valid value in a list, cyclically - Gribouillis - Mar-19-2018 You can use module bisect import bisect as bs def valid_indexes(seq): return [i for i, v in enumerate(seq) if v is not None] def prev_valid(valid, i): return valid[bs.bisect_left(valid, i) - 1] def next_valid(valid, i): return valid[bs.bisect_right(valid, i) % len(valid)] if __name__ == '__main__': example_values = [None, None, 't', None, 'f', None, None, 's', None, None] valid = valid_indexes(example_values) for i, v in enumerate(example_values): if v is not None: print("value at position {} is valid: {}".format(i,v)) else: print("value at position {} is invalid".format(i)) print(" previous valid value is at position {}".format(prev_valid(valid, i))) print(" next valid value is at position {}".format(next_valid(valid, i))) RE: getting previous/next valid value in a list, cyclically - studeo - Mar-20-2018 (Mar-19-2018, 11:03 PM)Gribouillis Wrote: You can use module bisect Thanks! Works perfectly. |