I have a simple task I want to do, I want to compare two arrays like this:
array 1 of data: [0.0, 92.6, 87.8, 668.8, 0.0, 86.3, 0.0, 147.1] array 2 of data: [16.7, 0.0, 0.0, 135.3, 0.0, 205.5, 0.0, 93.8] I want to find the difference between each value by index, and if the difference is lower than 50%, keep the value of the second array in a third array.
array 3 of data : [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 93.8] Now I believe using a for loop would be the best option, iterating through each array since both have the same length, the code below is part of my code, array1 and array2 are arrays like above.
for scan in sensor: #here is some code where we build the Array1 and Array 2 from serial data which is inside the loop. if specialcount ==1: #build the first array #get data from dict and build first array datadict={ 0:[], 1:[], 2:[], 3:[], 4:[], 5:[], 6:[], 7:[], } if specialcount ==2: #build the second array. #get data from dict and build second array datadict={ 0:[], 1:[], 2:[], 3:[], 4:[], 5:[], 6:[], 7:[], } if specialcount ==3: #after we build 2 arrays, specialcount goes to 3, and we now compare the arrays indexsizeofArray1 = len(array1) indexsizeofArray2 = len(array2) if indexsizeofArray1 != indexsizeofArray2: print ("array mismatch, try again") break elif indexsizeofArray1 == indexsizeofArray2: print("array match, proccessing") array3 = [0,0,0,0,0,0,0,0] for X in range (indexsizeofArray2): difference = array2[X]-array1[X] fiftypercent = array1[X]-(array1[X]/2) lowerrange = int(array1[X]-fiftypercent) upperrange = int(array1[X]+fiftypercent) if array2[X] in range (lowerrange,upperrange): array3[X]=array2[X] if X == indexsizeofArray2: print("final array: ",array3) specialcount +=1 Above is my code ,I do get the result I want of course, but I believe I could make it cleaner mostly because I am in the code transforming float to int as they work with the range function. I am sure there is a cleaner way of doing this , that specialcount==3 is my condition since I build the 2 arrays inside the forloop before as soon as the second array is built, then we, this is inside a loop where I am polling constantly a sensor, the sensor is special in the sense that if I stop polling it and go out of the loop, it will not work again unless I restart it , as such I must be inside this loop all the time.
I am sure I can improve the speed of it if slightly, mostly on the reinitialization of the datadict, since I initialized it before , however inside the loop I populate it with the data up until a certain point, in which I then create the first array of data, and clear it so I can rebuild it. The goal is to build two arrays of 2 different variables that must be related, once those 2 lists are built, I need to compare them, ensure that they are not that different and if they are not , pass it to another function , if its too different, the value for that index in the list is 0 .
specialcountis incremented every time through the loop. Theifblocks only execute for values 1 to 3. After that, it's an infinite loop that doesn't do anything. \$\endgroup\$code where we build the Array1I don't see that. I seedatadictset to a weird literal dict from 0…7 to empty lists - never to be used. \$\endgroup\$