I am trying to have a loop in Python like this, which makes the
if statement execute when the value is less than the lower threshold or greater than upper threshold:-
for i in x_range_original: for j in range(0,16): lower_threshold = min(df_log[:,j]) upper_threshold = max(df_log[:,j]) if df_log[i,j] < lower_threshold or df_log[i,j] > upper_threshold: print("Error detected")
However, what I would like to do is have conditions like this:-
if df_log[i,j] within 5 % of lower_threshold or df_log[i,j] within 5 % of upper_threshold: print("Error detected")
I know that this accomplished using long statements within the conditional and multiple conditions. But, is there a neat way (say a function which can do the part of within 5 %) to accomplish this, or any way we can do this in minimal lines of code?
You can use chained comparisons:
# lt,ut = lower_threshold, upper_threshold if((0.95*lt < df_log[i,j] < 1.05*lt) or (0.95*ut < df_log[i,j] < 1.05*ut)): print("Error detected")
We can even make it more pythonic as @wjandrea suggests below;
if any(0.95*t < df_log[i,j] < 1.05*t for t in [lt, ut]): print("Error detected")
- What is pip's `--no-cache-dir` good for?
- How do I iterate over object properties and assign user input to them?
- How to insert a custom PyQt5-widget with own layout in another widget with layout?
- How to do a Python split() on languages (like Chinese) that don't use whitespace as word separator?
- How to end for loop on tkinter askopenfilename when cancel is pressed?
- Detect if an image has a border, programmatically (return boolean)
- from wx.lib.pubsub import Publisher as pub ImportError: cannot import name Publisher
- Is it possible to pass a comparator to a PriorityQueue in python
- How simplify two neested loops in python
- copy data from csv to postgresql using python