![]() |
Get the correct value when comparing with the list - 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: Get the correct value when comparing with the list (/thread-4265.html) |
Get the correct value when comparing with the list - chris0147 - Aug-04-2017 Hi all, I am working on my code as I am calculating with the button width size and the pixel in the area of the X in the screen to add it together to get the value. I'm trying to compare the value with the list when I'm calculating them to see if the value would come close, higher or equal to in the list. Here is the code: CurrentRows = [375, 441, 507, 559, 610, 669, 724, 790, 838, 844, 849, 897, 910, 949, 959, 1009, 1016, 1018, 1019, 1072, 1125, 1138, 1184, 1186, 1189, 1238, 1246, 1286, 1419, 1620, 1762, 1840, 1943] nextprogram = int(program_id) + 1 nextprogram1 = int(nextprogram) + 1 nextprogram2 = int(nextprogram1) + 1 nextprogram3 = int(nextprogram2) + 1 program_button_1 = self.getControl(int(program_id)) program_button_2 = self.getControl(int(nextprogram)) program_button_3 = self.getControl(int(nextprogram1)) program_button_4 = self.getControl(int(nextprogram2)) pos_X = program_button_1.getX() width = program_button_1.getWidth() pos2_X = pos_X + width + 5 print "program2 posX............................" print pos2_X program_button_2.setPosition(pos2_X, pos_Y) for pos_X1 in CurrentRows: if pos_X1 >= pos_X: pos_X = pos_X1 break width = program_button_2.getWidth() pos3_X = pos2_X + width + 5 print "program3 posX............................" print pos3_X program_button_3.setPosition(pos3_X, pos_Y)When the variable pos_X show the value 1194 , the closest one would be 1184 . It is because 1194 went over 1184 so 1184 would be the correct value that I would use. If the value show 1253 , the closest one would be 1246 . When the value show 1125 that are matched from the list, I want to get 1125 from the list. How I could do that using with my code? Thanks in advance! RE: Get the correct value when comparing with the list - nilamo - Aug-04-2017 So, something like this? >>> def get_nearest(haystack, needle): ... haystack = iter(haystack) ... last = next(haystack) ... for item in haystack: ... if item > needle: ... return last ... last = item ... return last ... >>> CurrentRows = [375, 441, 507, 559, 610, 669, 724, 790, 838, 844, 849, 897, 910, 949, 959, 1009, 1016, 1018, 1019, 1072, 1125, 1138, 1184, 1186, 1189, 1238, 1246, 1286, 1419, 1620, 1762, 1840, 1943] >>> get_nearest(CurrentRows, 1194) 1189 >>> get_nearest(CurrentRows, 1253) 1246 >>> get_nearest(CurrentRows, 1125) 1125 RE: Get the correct value when comparing with the list - buran - Aug-04-2017 current_rows = [375, 441, 507, 559, 610, 669, 724, 790, 838, 844, 849, 897, 910, 949, 959, 1009, 1016, 1018, 1019, 1072, 1125, 1138, 1184, 1186, 1189, 1238, 1246, 1286, 1419, 1620, 1762, 1840, 1943] lookup = 1100 # number to compare with print max(filter(lambda x: x <= lookup, current_rows)) # using max would get correct value even if list is not sorted lookup = 1200 # number to compare with print filter(lambda x: x <= lookup, current_rows)[-1] # list (filtered one or the original one) MUST be sorted
RE: Get the correct value when comparing with the list - chris0147 - Aug-06-2017 (Aug-04-2017, 07:19 AM)buran Wrote: Thank you very much for this. RE: Get the correct value when comparing with the list - buran - Aug-06-2017 well, with lookup value between 1180 and 1183 the value you will get is 1138, i.e. that's closest value from the list , not exceeding the lookup value. i.e. lookup is the number you compare with the list looking for value from the list that is closest but not exceeding the lookup value (as per your original post) RE: Get the correct value when comparing with the list - chris0147 - Aug-06-2017 (Aug-06-2017, 09:44 PM)buran Wrote: well, with lookup value between 1180 and 1183 the value you will get is 1138, i.e. that's closest value from the list , not exceeding the lookup value. I know that, but I want to compare the values between 1180 and 1183 to get the value of 1184. Would I have to use something like this? lookup = max(filter(lambda x: x >= lookup, CurrentRows)) if lookup >= 1180 and lookup <= 1183 print lookupor this? lookup = max(filter(lambda x: x >= lookup, CurrentRows)) if lookup >= 1180 and lookup <= 1183 lookup = 1184 RE: Get the correct value when comparing with the list - chris0147 - Aug-07-2017 any idea????????????? RE: Get the correct value when comparing with the list - nilamo - Aug-07-2017 I don't understand. In your original post, you said you wanted the first number that wasn't larger. Now you want the first number that IS larger. It should be simple to switch either of the code samples provided to do that (flip a < to a >), but that might just cause an issue with different input. I think you need to more clearly define what it is, exactly, the output looks like for the various input. RE: Get the correct value when comparing with the list - chris0147 - Aug-09-2017 (Aug-07-2017, 09:10 PM)nilamo Wrote: I don't understand. In your original post, you said you wanted the first number that wasn't larger. Now you want the first number that IS larger. It should be simple to switch either of the code samples provided to do that (flip a < to a >), but that might just cause an issue with different input. I think you need to more clearly define what it is, exactly, the output looks like for the various input. Okay, what I am trying to do is to compare the value between 1180 and 1183. My best option is to forget with comparing on the list and just use the if statement to compare the value I want instead of checking the highest value with the list. |