Efficiently check if an element occurs at least n times in a list

后端 未结 7 2175
南旧
南旧 2020-12-07 00:23

How to best write a Python function (check_list) to efficiently test if an element (x) occurs at least n times in a list (l

7条回答
  •  北海茫月
    2020-12-07 01:17

    You could use the second argument of index to find the subsequent indices of occurrences:

    def check_list(l, x, n):
        i = 0
        try:
            for _ in range(n):
                i = l.index(x, i)+1
            return True
        except ValueError:
            return False
    
    print( check_list([1,3,2,3,4,0,8,3,7,3,1,1,0], 3, 4) )
    

    About index arguments

    The official documentation does not mention in its Python Tutuorial, section 5 the method's second or third argument, but you can find it in the more comprehensive Python Standard Library, section 4.6:

    s.index(x[, i[, j]]) index of the first occurrence of x in s (at or after index i and before index j(8)

    (8) index raises ValueError when x is not found in s. When supported, the additional arguments to the index method allow efficient searching of subsections of the sequence. Passing the extra arguments is roughly equivalent to using s[i:j].index(x), only without copying any data and with the returned index being relative to the start of the sequence rather than the start of the slice.

    Performance Comparison

    In comparing this list.index method with the islice(gen) method, the most important factor is the distance between the occurrences to be found. Once that distance is on average 13 or more, the list.index has a better performance. For lower distances, the fastest method also depends on the number of occurrences to find. The more occurrences to find, the sooner the islice(gen) method outperforms list.index in terms of average distance: this gain fades out when the number of occurrences becomes really large.

    The following graph draws the (approximate) border line, at which both methods perform equally well (the X-axis is logarithmic):

提交回复
热议问题