##### Find _ in a list
 Find _ in a list 1234 Programmer named Tim Posts: 10 Threads: 3 Joined: Nov 2020 Reputation: Dec-01-2020, 06:33 PM (This post was last modified: Dec-01-2020, 06:36 PM by 1234.) (Dec-01-2020, 05:56 PM)bowlofred Wrote: (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.When i changed the target to 111, the program returned 140. Why if in that list there is a 100, which is smaller than 111, and 140 isn't. Reply 1234 Programmer named Tim Posts: 10 Threads: 3 Joined: Nov 2020 Reputation: Dec-01-2020, 06:35 PM (This post was last modified: Dec-01-2020, 06:37 PM by 1234.) I've changed the target to 111, the program returned 140. Why if there is a 100, that is smaller, and 140 isn't? Reply deanhystad So-and-so of the Yard Posts: 2,156 Threads: 11 Joined: Feb 2020 Reputation: Dec-01-2020, 07:05 PM (This post was last modified: Dec-01-2020, 07:25 PM by deanhystad.) I'm guessing you have a typo? I just ran with target = 111 and got ``Output:Greatest number less than 111 is at position 5 with value 102``What are you using for data? I used `sorted_list = [1, 2, 5, 6, 100, 102, 110, 140, 145]`If I type -111 I get ``Output:Greatest number less than -111 is at position -1 with value 145``But the demo does warn to be on the lookout for index == -1. Reply bowlofred Da Bishop Posts: 1,194 Threads: 3 Joined: Mar 2020 Reputation: Dec-01-2020, 07:13 PM (Dec-01-2020, 06:35 PM)1234 Wrote: I've changed the target to 111, the program returned 140. Why if there is a 100, that is smaller, and 140 isn't? Can you show the output? Reply 1234 Programmer named Tim Posts: 10 Threads: 3 Joined: Nov 2020 Reputation: Dec-01-2020, 07:20 PM (This post was last modified: Dec-01-2020, 07:30 PM by 1234.) Yeah what I had was a typo, but 1 more thing, if for example there is a list(5,7,2,9,34) and I run the program with target = 7, the result will be 5, because it will only do lower that 7, instead of lower or equal, which will be 7. How would I change it from lower, to lower or equal? Reply bowlofred Da Bishop Posts: 1,194 Threads: 3 Joined: Mar 2020 Reputation: Dec-01-2020, 07:53 PM Change it from bisect_left to bisect_right. Now it will give you an insertion point to the right of your target. If you back up one spot (like the program does), that will return the largest number less than or equal to your target. Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Find Common Elements in 2 list quest 4 623 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 927 Dec-11-2020, 07:07 PM Last Post: tester_V Trying to find first 2 letter word in a list of words Oldman45 7 1,064 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 757 Jun-02-2020, 05:23 PM Last Post: bowlofred Find index value in List Martin2998 3 953 May-12-2020, 02:17 PM Last Post: deanhystad How to find something in a list using its index rix 1 785 Dec-20-2019, 04:12 PM Last Post: stullis why my method doesn't find my List in the same class? Scorpio 2 1,092 Jan-31-2019, 05:21 PM Last Post: Scorpio find any item in the list evilcode1 5 1,838 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,607 Mar-30-2018, 03:43 PM Last Post: csplrj Beginner Q: failure to find string in list Jenny 2 1,323 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