Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find _ in a list
#1
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
#2
Is it ok to sort the list first?
Reply
#3
(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
#4
Binary searches on sorted lists are very efficient.
Reply
#5
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
#6
Thanks,
quick question, in binary search how do you make the value that your looking for equal or smaller than 100?
Reply
#7
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
#8
(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
#9
Why don't you try it? You can run the posted code and change the target value.
ndc85430 likes this post
Reply
#10
(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
  Program to find Mode of a list PythonBoy 6 997 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,013 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Find (each) element from a list in a file tester_V 3 1,155 Nov-15-2022, 08:40 PM
Last Post: tester_V
  read a text file, find all integers, append to list oldtrafford 12 3,370 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,904 May-31-2022, 01:58 PM
Last Post: Larz60+
  Python Program to Find the Total Sum of a Nested List vlearner 8 4,786 Jan-23-2022, 07:20 PM
Last Post: menator01
  Find the highest value of a list Menthix 4 1,834 Oct-29-2021, 02:32 PM
Last Post: Menthix
  Find Common Elements in 2 list quest 4 2,684 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,583 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