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
  Program to find Mode of a list PythonBoy 6 998 Sep-12-2023, 09:31 AM
Last Post: PythonBoy
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,014 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Find (each) element from a list in a file tester_V 3 1,156 Nov-15-2022, 08:40 PM
Last Post: tester_V
  read a text file, find all integers, append to list oldtrafford 12 3,371 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  find some word in text list file and a bit change to them RolanRoll 3 1,482 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  How to find the second lowest element in the list? Anonymous 3 1,905 May-31-2022, 01:58 PM
Last Post: Larz60+
  Python Program to Find the Total Sum of a Nested List vlearner 8 4,788 Jan-23-2022, 07:20 PM
Last Post: menator01
  Find the highest value of a list Menthix 4 1,835 Oct-29-2021, 02:32 PM
Last Post: Menthix
  Find Common Elements in 2 list quest 4 2,685 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 3,585 Dec-11-2020, 07:07 PM
Last Post: tester_V

Forum Jump:

User Panel Messages

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