返回列表中递增开始的第一个元素的索引。

4
假设我有一个像这样的列表,在其中数字以不同的步长递增:
[ 0,  4,  6,  8, 12, 15, 19, 21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]

我希望返回列表中第一个增量为+1的元素的索引。在此示例中,23是从哪个位置开始增量变为+1的第一个位置,它的索引应为8,这就是我想要的输出。

有什么简单而优雅的方法可以实现这个目标?以下是我尝试过的代码:

>>> for (a,b) in zip(l, l[1:]):
...     if b-a == 1:
...             print(l.index(a))
...             break

更新:在这种特定的设置中,一旦增加变得增量化,它将继续保持这种状态。增加可能永远不会变成增量。


如果我将42添加到列表中,结果会改变吗? - Kelly Bundy
1
@KellyBundy 在这种特定的设置中,一旦增加变得渐进,它将继续保持这种状态。 - Clement Attlee
1
它是否可能永远不会变得增量? - Kelly Bundy
1
@KellyBundy 很好的问题。是的,这是可能的。 - Clement Attlee
6个回答

6

解决方案1:operator

from operator import sub, indexOf

L = [ 0,  4,  6,  8, 12, 15, 19, 21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]

print(indexOf(map(sub, L[1:], L), 1))
# prints 8

如果差值1从未出现,则会引发 ValueError: sequence.index(x): x not in sequence,因此可能需要使用try/except处理。

解决方案2: bisect

这个方法只需要O(log n)的时间,利用增量的单调性(如你所说 "一旦增量变为递增,它将继续保持这种状态")。

from bisect import bisect

L = [ 0,  4,  6,  8, 12, 15, 19, 21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]

class IsIncremental:
    def __getitem__(_, i):
        return L[i+1] - L[i] == 1

print(bisect(IsIncremental(), False, 0, len(L) - 1))
# prints 8

如果不存在差值为1,则打印len(L) - 1

顺便说一下...易读性

正如PEP 8所述:

永远不要使用字符'l'(小写字母"el")[...]作为单字符变量名。在某些字体中,这些字符与数字1和0无法区分。当想使用"l"时,请改用"L"。


同意,这很好。也许值得注意的是,如果未找到该值,则会引发异常。 - Mark
1
除了没有返回索引的情况,这确实非常优雅。 - Ryan Millares

1

步骤:

  1. 遍历数组直到倒数第二个元素。
  2. 检查下一个元素的值是否与当前元素的值相差1。
  3. 打印索引并退出循环。

代码:

my_list = [0, 4, 6, 8, 12, 15, 19, 21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]
for i in range(len(my_list)-1):
    if my_list[i+1] - my_list[i] == 1:
        print(i)
        break

结果:

8


0
使用for each循环并检查前一个值与当前值。一旦您达到当前值仅比前一个值大1的点,请返回数组中前一个值的索引:
myList = [ 0,  4,  6,  8, 12, 15, 19, 21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]
lastVal = -1000
for i in myList:
    if i - lastVal == 1:
        print(myList.index(lastVal)) #will print your desired value's index. If this is in a function, replace print with return
        break
    lastVal = i
if myList.index(lastVal) == len(myList) - 1:
    print("There is no incremental increase in your array")

(编辑,将返回更改为lastVal,修正为打印索引)输出:

8

0

这里是一种迭代的方法。我们可以循环遍历列表,并在每个索引处执行以下操作:

  • 如果当前值是前一个值加一,则不移动增量索引
  • 否则,将增量索引重置为当前位置

如果我们到达列表的末尾并且我们有一个增量索引早于最后一个位置,则我们有一个潜在的匹配。

lst = [0,  4,  6,  8, 12, 15, 19, 21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]
idx = 0
for i in range(1, len(lst)):
    if lst[i] != lst[i-1] + 1:
        idx = i

if idx < len(lst) - 1:
    print("Found index: " + str(idx) + ", value: " + str(lst[idx]))
else:
    print("No incremental index found")

这将打印:

Found index: 8, value: 23

0
这是使用列表推导的一种方法。
lst = [ 0,  4,  6,  8, 12, 15, 19, 21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]
list2 = [i-1 for i,j in enumerate(lst) if j-lst[i-1]==1] 
if len(list2)>0:
    print(list2[0])
else:
    print('No one up number exists')

0

与之前的答案类似。

myList = [0, 4, 6, 8, 12, 15, 19, 21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]
l0 = 0 #suppose that the initial value is 0
for l1 in myList:
    increment = l1 - l0
    if increment == 1:
        print(myList.index(l0)) #if you want to get the second element, use l1 instead.
        break #if you want to get all the first elements that has 1 increment, remove break
    l0 = l1 #memorize l1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接