Python Forum
Get the correct value when comparing with the list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get the correct value when comparing with the list
#1
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!
Reply
#2
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
Reply
#3
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
Output:
1072 1189
Reply
#4
(Aug-04-2017, 07:19 AM)buran Wrote: Thank you very much for this.

In this line:

print max(filter(lambda x: x <= lookup, current_rows)) # using max would get correct value even if list is not sorted
If I have the value between 1180 and 1183, how I can compare it to get the correct value of 1184?


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
Output:
1072 1189
Reply
#5
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)
Reply
#6
(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.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)

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 lookup
or this?

lookup = max(filter(lambda x: x >= lookup, CurrentRows))
if lookup >= 1180 and lookup <= 1183
   lookup = 1184
Reply
#7
any idea?????????????
Reply
#8
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.
Reply
#9
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Comparing List values to get indexes Edward_ 7 1,138 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  Select correct item from list for subprocess command pythonnewbie138 6 3,291 Jul-24-2020, 09:09 PM
Last Post: pythonnewbie138
  Problem with comparing list palladium 2 2,145 Jul-08-2020, 10:49 AM
Last Post: DeaD_EyE
  comparing 2 dimensional list glennford49 10 4,128 Mar-24-2020, 05:23 PM
Last Post: saikiran
  What is the correct syntax for list items that need to contain a quotation mark, etc? KaisoArt 7 3,486 Sep-14-2019, 05:26 AM
Last Post: buran
  Looping through dictionary and comparing values with elements of a separate list. Mr_Keystrokes 5 3,880 Jun-22-2018, 03:08 PM
Last Post: wavic
  Create a new list by comparing values in a list and string DBS 2 3,520 Jan-14-2017, 07:59 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020