Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 查找列表中的项的索引,其中列表开始与索引之和大于X_List_Python 3.x_Sum - Fatal编程技术网

List 查找列表中的项的索引,其中列表开始与索引之和大于X

List 查找列表中的项的索引,其中列表开始与索引之和大于X,list,python-3.x,sum,List,Python 3.x,Sum,我正在寻找以下代码的快速实现;例如,使用map()或next(): 当列表开始与索引之和大于14时,代码打印列表中项目的索引 注意:我需要在另一个循环中不断更新链接。因此,numpy中的解决方案可能会太慢,因为它无法就地更新列表。您还可以与enumerate()和next()一起使用: 是什么让你认为map或next会更快?函数式编程不适用于这样的累加算法。 l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] total_so_far = 0 for i in l:

我正在寻找以下代码的快速实现;例如,使用map()或next():

当列表开始与索引之和大于14时,代码打印列表中项目的索引

注意:我需要在另一个循环中不断更新链接。因此,numpy中的解决方案可能会太慢,因为它无法就地更新列表。

您还可以与
enumerate()
next()一起使用:


是什么让你认为
map
next
会更快?函数式编程不适用于这样的累加算法。
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

total_so_far = 0
for i in l:
    total_so_far += i
    if total_so_far > 14:
        break

print(i)
In [1]: from itertools import takewhile

In [2]: l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [3]: next(index for index, value in enumerate(accumulate(l)) if value > 14)
Out[3]: 5