Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find _ in a list
#11
(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
#12
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
#13
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
#14
(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
#15
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
#16
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 779 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 1,072 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,159 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 846 Jun-02-2020, 05:23 PM
Last Post: bowlofred
  Find index value in List Martin2998 3 1,048 May-12-2020, 02:17 PM
Last Post: deanhystad
  How to find something in a list using its index rix 1 842 Dec-20-2019, 04:12 PM
Last Post: stullis
  why my method doesn't find my List in the same class? Scorpio 2 1,200 Jan-31-2019, 05:21 PM
Last Post: Scorpio
  find any item in the list evilcode1 5 1,992 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,779 Mar-30-2018, 03:43 PM
Last Post: csplrj
  Beginner Q: failure to find string in list Jenny 2 1,398 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