修改
程序的期望行为是找到具有增加趋势的数字序列,因此我想从ks
列表生成如下列表:
desiredList=[[97,122],[98,111],[98,101,103,103,104]]
我有以下内容,我的目标是根据列表的长度运行for循环,在for循环内部更改列表长度。 Python仅考虑for循环之前的长度,当循环中更改列表的长度时,它仍然在循环之前采用较旧的值。这是代码:
ks=[97,122,111,98,111,98,101,103,103,104,97]
splitLine=2
counter=[]
for i in range(0,len(ks)):
a=ks[i:splitLine]
while len(a)>1:
for j in range(0,len(a)):
m=j
n=j+1
if(a[m]-a[n]<=0):
c=c+1
k=splitLine+c-1
a.append(ks[k]) #When append happens, the for loop still takes the older value of len(a) instead of new value
else:
a.pop(-1)
counter.append(a)
splitLine=splitLine+1
a=[]
break
答案 0 :(得分:1)
您的实现可能会为它正在尝试解决的问题嵌套太多循环。
第一个实现包含错误。请参阅下面的修补程序。
尝试沿着这些方向做点什么:
l = [97,122,111,98,111,98,101,103,103,104,97]
out = []
acc = []
for v in l:
if len(acc)==0 or v >= acc[-1]:
acc.append(v)
else:
if len(acc) > 1:
out.append(acc)
acc = [v]
print(out)
>>>[[97, 122], [98, 111], [98, 101, 103, 103, 104]]
以前的代码很慢,可以删除最后找到的片段。我在运行随机测试时发现错误,尝试优化版本。以下代码显示了带有更正的原始代码和优化版本,可以快30%。
def original(l):
out = []
acc = []
added = False
for v in l:
if len(acc)==0 or v >= acc[-1]:
acc.append(v)
else:
added = False
acc = [v]
if acc is not None and len(acc)>1 and not added:
added = True
out.append(acc)
return out
def optimized(l):
out = []
acc = None
tmp = None
deb_v = False
for v in l:
prev = acc[-1] if (acc is not None and len(acc)) else tmp
if prev is not None and v >= prev:
if tmp is not None:
acc = []
acc.append(tmp)
out.append(acc)
tmp = None
acc.append(v)
else:
acc = None
tmp = v
return out
# The original test data
l = [97,122,111,98,111,98,101,103,103,104,97]
assert original(l) == optimized(l) == [[97,122],[98,111],[98,101,103,103,104]]
# A list that triggered last-fragment-dropped error
l = [57, 16, 6, 19, 40, 3, 4, 13, 2, 70, 85, 65, 32, 69, 54, 51, 95, 74, 92, 46, 45, 26, 0, 61, 99, 43, 67, 71, 97, 10, 18, 73, 88, 47, 33, 82, 25, 75, 93, 80, 23, 37, 87, 90, 49, 15, 35, 63, 17, 64, 5, 72, 89, 21, 50, 8, 41, 86, 31, 78, 52, 76, 56, 42, 77, 36, 11, 60, 39, 22, 68, 27, 24, 28, 59, 96, 29, 38, 12, 79, 53, 9, 83, 94, 34, 14, 7, 48, 30, 20, 66, 62, 91, 58, 81, 1, 98, 44, 55, 84]
assert original(l) == optimized(l)
# Random testing
import random
l = list(range(100))
random.shuffle(l)
assert original(l) == optimized(l)
# Timing!
import timeit
print(timeit.timeit("original(l)", globals={"l":l, "original": original}))
# 43.95869998800117
print(timeit.timeit("optimized(l)", globals={"l":l, "optimized": optimized}))
# 34.82134292599949
答案 1 :(得分:0)
针对循环问题的快速解决方法是将for
循环替换为while
循环。改变这个:
for j in range(0,len(a)):
# <loop contents>
到此:
j = 0
while j < len(a):
# <loop contents>
j += 1
for
循环从j
中获取range
的值(Python 2中的list
和Python 3中的生成器对象)。第一次运行range
循环时计算此for
;无论你对a
做了什么,它都不会在那之后更新。
while
循环可以在这种情况下为您提供更多控制,因为您可以指定退出循环的条件。
答案 2 :(得分:0)
正如Moinuddin所说,问题的根源对我们来说并不清楚。但是,下面的代码显示了如何在长度发生变化时继续迭代列表:
def iterate_for_static_list_length(l):
for i in range(len(l)):
yield i
l.append(object())
def iterate_for_dynamic_list_length(l):
for i, _ in enumerate(l):
yield i
l.append(object())
if __name__ == '__main__':
l = [object()] * 3
print('Static implementation')
for value in iterate_for_static_list_length(l):
input(value)
print('\nDynamic implementation')
for value in iterate_for_dynamic_list_length(l):
input(value)
<强>输出强>
Static implementation
0
1
2
Dynamic implementation
0
1
2
3
4
5
6
7
8
这个程序将永远持续下去。在你的代码中,我可以看到你有条件地追加到循环中的列表,所以它似乎应该最终终止。