##### Find _ in a list
 Find _ in a list 1234 Programmer named Tim Posts: 10 Threads: 3 Joined: Nov 2020 Reputation: Nov-30-2020, 04:31 PM Hi, I was wondering is there a way that you can find the largest number in a list that is equal or smaller than my chosen number, without going through the whole list every time I want to do it? Is there a way that you can only include numbers that are equal or smaller than the number that I want. Reply deanhystad Da Bishop Posts: 1,927 Threads: 10 Joined: Feb 2020 Reputation: Nov-30-2020, 04:44 PM Is it ok to sort the list first? Reply 1234 Programmer named Tim Posts: 10 Threads: 3 Joined: Nov 2020 Reputation: Nov-30-2020, 07:21 PM (Nov-30-2020, 04:44 PM)deanhystad Wrote: Is it ok to sort the list first? Yes, my list is very big, and there is thousands of numbers bigger than the ones from list 'a', so even if the list is sorted it will take a lot of steps/time to find the biggest number that is equal or smaller than the one from list 'a'. Reply deanhystad Da Bishop Posts: 1,927 Threads: 10 Joined: Feb 2020 Reputation: Nov-30-2020, 08:16 PM Binary searches on sorted lists are very efficient. Reply bowlofred Da Bishop Posts: 1,091 Threads: 3 Joined: Mar 2020 Reputation: Nov-30-2020, 08:36 PM Once sorted, there are tools to find the the one you want efficiently, such as bisect. On a list of 10000000 ints, my machine took about 0.3 seconds to scan the list linearly and find the max value less than a target. It took 2 seconds to sort the list, but afterward bisect could find the highest value less than a target in 0.0001 seconds. Reply 1234 Programmer named Tim Posts: 10 Threads: 3 Joined: Nov 2020 Reputation: Dec-01-2020, 03:19 PM Thanks, quick question, in binary search how do you make the value that your looking for equal or smaller than 100? Reply bowlofred Da Bishop Posts: 1,091 Threads: 3 Joined: Mar 2020 Reputation: Dec-01-2020, 04:51 PM ```import bisect target = 100 sorted_list = [1, 2, 5, 6, 100, 102, 110, 140, 145] pos = bisect.bisect_left(sorted_list, target) # where to insert, so the number less than is to the left. # Beware of negative if you asked for a number less than any in the list print(f"Greatest number less than {target} is at position {pos-1} with value {sorted_list[pos-1]}")`````Output:Greatest number less than 100 is at position 3 with value 6`` Reply 1234 Programmer named Tim Posts: 10 Threads: 3 Joined: Nov 2020 Reputation: Dec-01-2020, 05:45 PM (Dec-01-2020, 04:51 PM)bowlofred Wrote: ```import bisect target = 100 sorted_list = [1, 2, 5, 6, 100, 102, 110, 140, 145] pos = bisect.bisect_left(sorted_list, target) # where to insert, so the number less than is to the left. # Beware of negative if you asked for a number less than any in the list print(f"Greatest number less than {target} is at position {pos-1} with value {sorted_list[pos-1]}")`````Output:Greatest number less than 100 is at position 3 with value 6``And what if my target is 100 and there is no 100 in the list, so I want to take the closest number to 100 that is smaller than 100? Reply deanhystad Da Bishop Posts: 1,927 Threads: 10 Joined: Feb 2020 Reputation: Dec-01-2020, 05:48 PM Why don't you try it? You can run the posted code and change the target value. ndc85430 likes this post Reply bowlofred Da Bishop Posts: 1,091 Threads: 3 Joined: Mar 2020 Reputation: Dec-01-2020, 05:56 PM (Dec-01-2020, 05:45 PM)1234 Wrote: And what if my target is 100 and there is no 100 in the list, so I want to take the closest number to 100 that is smaller than 100? The program didn't return 100 (because 100 isn't less than 100). It returned the largest number less than 100. You can read more about the specifics of bisect (which allow you to quickly find things in a sorted list, and to append things in a way that keeps a list sorted) in the docs. Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Find Common Elements in 2 list quest 4 109 Apr-14-2021, 03:57 PM Last Post: quest List of error codes to find (and count) in all files in a directory tester_V 8 514 Dec-11-2020, 07:07 PM Last Post: tester_V Trying to find first 2 letter word in a list of words Oldman45 7 802 Aug-11-2020, 08:59 AM Last Post: Oldman45 How to find the first and last of one of several characters in a list of strings? tadsss 2 544 Jun-02-2020, 05:23 PM Last Post: bowlofred Find index value in List Martin2998 3 713 May-12-2020, 02:17 PM Last Post: deanhystad How to find something in a list using its index rix 1 648 Dec-20-2019, 04:12 PM Last Post: stullis why my method doesn't find my List in the same class? Scorpio 2 910 Jan-31-2019, 05:21 PM Last Post: Scorpio find any item in the list evilcode1 5 1,379 Oct-08-2018, 01:36 PM Last Post: snippsat How to find the list of dependencies not present in the remote artifactory repository csplrj 6 2,160 Mar-30-2018, 03:43 PM Last Post: csplrj Beginner Q: failure to find string in list Jenny 2 1,152 Mar-18-2018, 01:07 PM Last Post: Jenny

Forum Jump:

### User Panel Messages

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