我试图创建一个函数,该函数返回沿其左侧的所有元素的总和大于数字x
的最小元素的索引。使用列表元素的总和查找列表的索引
我该如何以Pythonic的方式做到这一点?
例子:
我有一个数字[32, 6, 12]
的名单,我试图让一个函数将返回0
如果x < 32
,1
如果x < 32 + 6
和2
如果x < 32 + 6+ 12
非Python的方式将是:
a = [23,3,32]
i = 0
summ = 0
found = False
def example(e):
while not found:
if e <= (a[i] + summ):
found = True
element = i
else:
summ += a[i]
i += 1
return element
究竟什么是你的问题? – jonrsharpe 2014-10-20 11:23:11
@jonrsharpe如何以pythonic的方式做到这一点 – confused00 2014-10-20 11:23:57
这不是一个代码写作服务;如果你想检查你的代码(*假设它已经过测试并正在工作*),请尝试http://codereview.stackexchange.com。 – jonrsharpe 2014-10-20 11:24:48