简体   繁体   English

第一个大于 x 的 Python 列表索引?

[英]First Python list index greater than x?

What would be the most Pythonic way to find the first index in a list that is greater than x?在大于 x 的列表中查找第一个索引的最 Pythonic 方法是什么?

For example, with例如,与

list = [0.5, 0.3, 0.9, 0.8]

The function功能

f(list, 0.7)

would return会回来

2.
next(x[0] for x in enumerate(L) if x[1] > 0.7)

如果 list 已排序,则bisect.bisect_left(alist, value)对于大列表比next(i for i, x in enumerate(alist) if x >= value)更快。

>>> alist= [0.5, 0.3, 0.9, 0.8]
>>> [ n for n,i in enumerate(alist) if i>0.7 ][0]
2
filter(lambda x: x>.7, seq)[0]
for index, elem in enumerate(elements):
    if elem > reference:
        return index
raise ValueError("Nothing Found")

1) NUMPY ARGWHERE, general lists 1) NUMPY ARGWHERE,一般列表

If you are happy to use numpy (imported as np here), then the following will work on general lists (sorted or unsorted):如果您乐于使用 numpy(在此处作为np导入),那么以下内容将适用于一般列表(已排序或未排序):

np.argwhere(np.array(searchlist)>x)[0]

or if you need the answer as a integer index:或者如果您需要将答案作为整数索引:

np.argwhere(np.array(searchlist)>x)[0][0]

2) NUMPY SEARCHSORTED, sorted lists (very efficient for searching lists within a list) 2)NUMPY SEARCHSORTED,排序列表(在列表中搜索列表非常有效)

However, if your search list [l1,l2,...] is sorted, it is much cleaner and nicer to use the function np.searchsorted :但是,如果您的搜索列表 [l1,l2,...] 已排序,则使用函数np.searchsorted会更清晰、更好:

np.searchsorted(searchlist,x)

The nice thing about using this function is that as well as searching for a single value x within the search list [l1,l2,...], you can also search for a list of values [x1,x2,x3...xn] within your search list (ie x can be a list too, and it is extremely efficient relative to a list comprehension in this case ).使用此函数的好处在于,除了在搜索列表 [l1,l2,...] 中搜索单个值 x 之外,您还可以搜索值列表 [x1,x2,x3... xn] 在您的搜索列表中(即 x 也可以是一个列表, 在这种情况下,它相对于列表推导非常有效)。

另一个:

map(lambda x: x>.7, seq).index(True)

I know there are already plenty of answers, but I sometimes I feel that the word pythonic is translated into 'one-liner'.我知道已经有很多答案了,但有时我觉得pythonic这个词被翻译成“单线”。

When I think a better definition is closer to this answer :当我认为更好的定义更接近这个答案时:

"Exploiting the features of the Python language to produce code that is clear, concise and maintainable." “利用 Python 语言的特性来生成清晰、简洁和可维护的代码。”

While some of the above answers are concise, I do not find them to be clear and it would take a newbie programmer a while to understand, therefore not making them extremely maintainable for a team built of many skill levels.虽然上面的一些答案很简洁,但我觉得它们不是很清楚,新手程序员需要一段时间才能理解,因此对于一个由许多技能水平组成的团队来说,它们并不是非常易于维护。

l = [0.5, 0.3, 0.9, 0.8]

def f(l, x):
    for i in l:
        if i >x: break
    return l.index(i)


f(l,.7)

or或者

l = [0.5, 0.3, 0.9, 0.8]

def f(l, x):
    for i in l:
        if i >x: return l.index(i)



f(l,.7)

I think the above is easily understood by a newbie and is still concise enough to be accepted by any veteran python programmer.我认为上面的内容对于新手来说很容易理解,并且仍然足够简洁,可以被任何资深的 Python 程序员所接受。

I think writing dumb code is a positive.我认为编写愚蠢的代码是积极的。

I had similar problem when my list was very long.当我的列表很长时,我遇到了类似的问题。 Comprehension or filter-based solutions would go through the whole list.理解或基于过滤器的解决方案将遍历整个列表。 Instead itertools.takewhile will break the loop once the condition is false for the first time:相反,一旦条件第一次为假, itertools.takewhile将打破循环:

from itertools import takewhile

def f(l, b): return len([x for x in takewhile(lambda x: x[1] <= b, enumerate(l))])

l = [0.5, 0.3, 0.9, 0.8]
f(l, 0.7)
>>> f=lambda seq, m: [ii for ii in xrange(0, len(seq)) if seq[ii] > m][0]
>>> f([.5, .3, .9, .8], 0.7)
2

You could also do this using numpy :您也可以使用numpy执行此操作:

import numpy as np

list(np.array(SearchList) > x).index(True)

Try this one:试试这个:

def Renumerate(l):
    return [(len(l) - x, y) for x,y in enumerate(l)]

example code:示例代码:

Renumerate(range(10))

output:输出:

(10, 0)
(9, 1)
(8, 2)
(7, 3)
(6, 4)
(5, 5)
(4, 6)
(3, 7)
(2, 8)
(1, 9)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 第一个熊猫DataFrame列索引大于x - First pandas DataFrame column index greater than x 找到 x 中大于 0.999 的第一个值的索引 - find the index of the first value in x that is greater than 0.999 在Python中,如何在排序列表中找到大于阈值的第一个值的索引? - In Python, how do you find the index of the first value greater than a threshold in a sorted list? 我如何检查第一个列表索引 1 是否大于第二个列表索引 0,以及第一个列表索引 2 是否大于第二个列表索引 1。依此类推 - How can i check in first List index 1 is greater than second list index 0, and first List index 2 is greater than second list index 1. and so on 返回 Python 中列表中每个子列表的大于 k 的数字索引 - Return index of numbers greater than k for each of sublists in list in Python 计算列表中的对,使其第一个索引小于第二个但第一个元素大于第二个 - Counting the pairs in a list such that their first index is less than the second but the first element is greater than the second 在python的列表中查找大于0的第一个和最后一个值 - Find first and last value greater than 0 in a list in python 如果在Python中大于x,则将浮点列表转换为最接近的整数 - Convert a list of floats to the nearest whole number if greater than x in Python Numpy - 对于 array1 中的每个元素 X,在 array2 中找到大于 X 的第一个元素的索引 - Numpy - for each element X in array1 find index of first element greater than X in array2 一周的第一个日期大于x - first date of week greater than x
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM