To find the first start of a particular condition in a sorted list in Python

Asked 2 years ago, Updated 2 years ago, 111 views

Given a sorted list like list1=[1,4,6,8,11,13,16] I would like to know how to get a list of list1 consisting only of 9 or higher.

I'm thinking of two ways.

First, list1=filter (lambdax: x>=9, list1)

Second, list1.append(9) list1.sort() idx=list1.index(9) list1=list1[idx+1]:

Third, binary search

What I am dissatisfied with is that the methods of 1 and 2 use time inefficiently because they cannot take advantage of the fact that the list is already sorted. You don't have to scan all the elements, but you end up scanning them all.

The method of 3 will have a longer code.

I think there's an easy and efficient way to find it! O(n) notation would be maximum log, but aside from these formalities, we actually need to save time. I think there will be a way if you use the built-in function well.

I'd appreciate it if you let me know

list search

2022-09-22 20:43

1 Answers

I only save time. In other words, if it is a performance issue, it is best to use the expansion module with c.

Python's built-in functions are also universal and convenient to use, but may not be good when it comes to efficiency. Rather, creating and using a function is often a good performance advantage.

def getListIndexByValue(L, value):
    for i, v in enumerate(L):
        if v > value:
            return i

list1=[1,4,6,8,11,13,16]
list1[getListIndexByValue(list1, 9):]
[11, 13, 16]

Cython Python Performance Comparison

#cython function (-O3)
def getListIndexByValue_C(list L, int value):
    cdef int i, v
    for i, v in enumerate(L):
        if v > value:
            return i

#python
def getListIndexByValue(L, value):
    for i, v in enumerate(L):
        if v > value:
            return i

timeit -n10000 getListIndexByValue_C(list1, 9)
10000 loops, best of 3: 61.9 ns per loop

timeit -n10000 getListIndexByValue(list1, 9)
10000 loops, best of 3: 554 ns per loop

There is a difference in performance about 9 times even though you only designated the type as cyton.

I want you to recognize that Python is focused on productivity, not on performance from the beginning. In other words, rather than improving performance on Python, it's better to keep the Python code, but write the bottleneck in c.


2022-09-22 20:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.