如何中断并从我在嵌套循环中结束的地方开始
How to break and start from where I ended in a nested loop
我想 运行 这个循环遍历一个列表,基本上它会在我的范围内搜索一个数字,直到它发现它在接下来的迭代中搜索下一个数字,但它会重新开始
这是我的代码
z = [30,84,126,135,137,179,242,342,426]
c=[]
for m in z:
for i in range(1,1002, 7):
if m in range(i, i+7):
c.append(m%7)
break
elif m not in range(i, i+7):
c.append(0)
print len(c) # outputs 246
但是 len(c) 应该等于 143,我该如何解决这个问题?
我想我明白你想做什么,最好的办法是改变你增加搜索范围的方式。
z = [30,84,126,135,137,179,242,342,426]
c=[] # initialize results array
i = 1 # initialize i
for m in z: # for each item in list
while 1: # perform this action until loop breaks
if m in range(i, i+7): #if m is in range
c.append(m%7)
break #break the while loop, moving on to the next item
elif m not in range(i, i+7):
c.append(0)
i = i+7 #increment the search range, but do not break the loop
#Display results
print len(c)
print c
因此,在您的原始代码中,您为数组 z
中的每个元素重置了搜索范围 i
。这就是为什么您的 len(c)
值比预期高很多。在我的代码中,当我遍历值数组时,我只将 i 从 1 迭代到 1002 一次。
如果这不能解决您的问题,请告诉我,我能够匹配您描述的功能,但不能匹配 len(c)
的预期输出。如果你想获得预期的价值,你可以改变代码来匹配这个:
z = [30,84,126,135,137,179,242,342,426]
c=[] # initialize results array
i = 1 # initialize i
for m in z: # for each item in list
while i<1002: # perform this action until loop breaks
if m in range(i, i+7): #if m is in range
c.append(m%7)
i = i+7
break # break the while loop, moving on to the next item
elif m in range(i-7, i):
break
else:
c.append(0)
i = i+7 # increment the search range, but do not break the loop
while i<1002: # finish iterating i all the way up to 1002
c.append(0)
i = i+7
#Display results
print len(c)
print c
其中 len(c)
为 143。
也许你想要的是generator
https://docs.python.org/2/howto/functional.html#generator-expressions-and-list-comprehensions
z = [30,84,126,135,136,137,179,242,342,426]
c = []
def counter(maximum, inc): # resetable generator from doc example
i = 1
while i < maximum:
val = (yield i)
# If value provided, change counter
if val is not None:
i = val
else:
i += inc
ig = counter(1002, 7)
for m in z:
for i in ig:
# catch multiple nums in same range
if m < i:
clast = c.pop()
# inline if-else inside append converts int to tuple to add m to
c.append((clast if type(clast) == tuple else (clast,)) + (m,))
# reset ig count
ig.send(i - 7)
break
if i <= m < i+7:
c.append(m)
break
else:
c.append(0)
# exhaust ig if you really want full count = 143
for i in ig:
c.append(0)
print(len(c))
在相同的时间间隔内添加了捕捉数字,需要可重置的生成器
解决了我所知道的最后两个问题:
现在为单个范围内的多个数字创建一个平面元组
通过将 ig 重置为 i - 7
来正确计数
虽然 generator
似乎回答了这个问题,但对于编程问题还有一个更好的工具:itertools.groupby
from itertools import groupby
z = [1,2,3, 30,84,126,135,136,137,140,141,179,242,342,426]
g = dict([[k, [*g]] for k, g in groupby(z, key=lambda x: (x-1)//7)])
d = [((tuple(g[i]) if len(g[i]) > 1 else g[i][0]) if (i in g) else 0)
for i in range(0, 143)]
运行 针对我的第一个答案的代码:(请使用相同的 z
,它已被更改)
c == d
Out[278]: True
看看匹配度如何 itertools.groupby
查看 dict wrapped groupby 结果:
g
Out[279]:
{0: [1, 2, 3],
4: [30],
11: [84],
17: [126],
19: [135, 136, 137, 140],
20: [141],
25: [179],
34: [242],
48: [342],
60: [426]}
(以上在 3.6 中有效,[*g]
和 dictionary key
测试 (i in g)
在 2.7 中可能不同)
我想 运行 这个循环遍历一个列表,基本上它会在我的范围内搜索一个数字,直到它发现它在接下来的迭代中搜索下一个数字,但它会重新开始
这是我的代码
z = [30,84,126,135,137,179,242,342,426]
c=[]
for m in z:
for i in range(1,1002, 7):
if m in range(i, i+7):
c.append(m%7)
break
elif m not in range(i, i+7):
c.append(0)
print len(c) # outputs 246
但是 len(c) 应该等于 143,我该如何解决这个问题?
我想我明白你想做什么,最好的办法是改变你增加搜索范围的方式。
z = [30,84,126,135,137,179,242,342,426]
c=[] # initialize results array
i = 1 # initialize i
for m in z: # for each item in list
while 1: # perform this action until loop breaks
if m in range(i, i+7): #if m is in range
c.append(m%7)
break #break the while loop, moving on to the next item
elif m not in range(i, i+7):
c.append(0)
i = i+7 #increment the search range, but do not break the loop
#Display results
print len(c)
print c
因此,在您的原始代码中,您为数组 z
中的每个元素重置了搜索范围 i
。这就是为什么您的 len(c)
值比预期高很多。在我的代码中,当我遍历值数组时,我只将 i 从 1 迭代到 1002 一次。
如果这不能解决您的问题,请告诉我,我能够匹配您描述的功能,但不能匹配 len(c)
的预期输出。如果你想获得预期的价值,你可以改变代码来匹配这个:
z = [30,84,126,135,137,179,242,342,426]
c=[] # initialize results array
i = 1 # initialize i
for m in z: # for each item in list
while i<1002: # perform this action until loop breaks
if m in range(i, i+7): #if m is in range
c.append(m%7)
i = i+7
break # break the while loop, moving on to the next item
elif m in range(i-7, i):
break
else:
c.append(0)
i = i+7 # increment the search range, but do not break the loop
while i<1002: # finish iterating i all the way up to 1002
c.append(0)
i = i+7
#Display results
print len(c)
print c
其中 len(c)
为 143。
也许你想要的是generator
https://docs.python.org/2/howto/functional.html#generator-expressions-and-list-comprehensions
z = [30,84,126,135,136,137,179,242,342,426]
c = []
def counter(maximum, inc): # resetable generator from doc example
i = 1
while i < maximum:
val = (yield i)
# If value provided, change counter
if val is not None:
i = val
else:
i += inc
ig = counter(1002, 7)
for m in z:
for i in ig:
# catch multiple nums in same range
if m < i:
clast = c.pop()
# inline if-else inside append converts int to tuple to add m to
c.append((clast if type(clast) == tuple else (clast,)) + (m,))
# reset ig count
ig.send(i - 7)
break
if i <= m < i+7:
c.append(m)
break
else:
c.append(0)
# exhaust ig if you really want full count = 143
for i in ig:
c.append(0)
print(len(c))
在相同的时间间隔内添加了捕捉数字,需要可重置的生成器
解决了我所知道的最后两个问题: 现在为单个范围内的多个数字创建一个平面元组 通过将 ig 重置为 i - 7
来正确计数虽然 generator
似乎回答了这个问题,但对于编程问题还有一个更好的工具:itertools.groupby
from itertools import groupby
z = [1,2,3, 30,84,126,135,136,137,140,141,179,242,342,426]
g = dict([[k, [*g]] for k, g in groupby(z, key=lambda x: (x-1)//7)])
d = [((tuple(g[i]) if len(g[i]) > 1 else g[i][0]) if (i in g) else 0)
for i in range(0, 143)]
运行 针对我的第一个答案的代码:(请使用相同的 z
,它已被更改)
c == d
Out[278]: True
看看匹配度如何 itertools.groupby
查看 dict wrapped groupby 结果:
g
Out[279]:
{0: [1, 2, 3],
4: [30],
11: [84],
17: [126],
19: [135, 136, 137, 140],
20: [141],
25: [179],
34: [242],
48: [342],
60: [426]}
(以上在 3.6 中有效,[*g]
和 dictionary key
测试 (i in g)
在 2.7 中可能不同)