與此斗爭了一段時間,但我如何能夠通過 X 跳過 python 中的回圈并繼續 Y 然后重復。
例如:
# This loop should loop until 99, but after it reaches a number that is a multiple of 10 e.g. 10,20,30 etc it should continue for 5 iterations, then skip to next multiple of 10.
# E.g 0,1,2,3,4,5,10,11,12,13,14,15,20,21,22,23,24,25,30...etc
for page_num in range(100):
# Use page_num however
uj5u.com熱心網友回復:
修改回圈以使用 的步驟10,并添加一個子回圈以使迭代在第 5 個元素之后中斷。
for j in range(0,100,10):
for page_num in range(j, j 6):
# use page_num
uj5u.com熱心網友回復:
continue如果單位位中的數字大于 5,則用于跳過回圈的其余部分。
skip_after = 5
for page_num in range(100):
if page_num % 10 > skip_after: continue
# ... Do the rest of your loop
print(page_num)
page_num % 10使用模%運算子給出除以page_num10 的余數(這是個位中的數字)。
輸出(為了便于閱讀,合并成一行):
0 1 2 3 4 5 10 11 12 13 14 15 20 21 22 23 24 25 30
uj5u.com熱心網友回復:
使用itertools.filterfalse
from itertools import filterfalse
for x in (filterfalse(lambda x: (x % 10) > 5, range(0, 100))):
print(x, end=' ')
輸出:
0 1 2 3 4 5 10 11 12 13 14 15 20 21 22 23 24 25 30 31 32 33 34 35 40 41 42 43 44 45 50 51 52 53 54 55 60 61 62 63 64 65 70 71 72 73 74 75 80 81 82 83 84 85 90 91 92 93 94 95
uj5u.com熱心網友回復:
我會推薦串列理解。如果這是完整的答案,我不肯定,但我會這樣做:
def find_divisors_2(page_num):
"""
You can insert an algorithmic phrase after if and it will produce the answer.
"""
find_divisors_2 = [x for x in range(100) if (Insert your algorithm)]
return find_divisors_2
uj5u.com熱心網友回復:
可以在一定條件下直接修改回圈page_num內部的值:for
for page_num in range(100):
if str(page_num)[-1]=="6":
page_num = 4
print(page_num)
uj5u.com熱心網友回復:
x = 4 # How many times to skip
y = 6 # How many times to run
t = [0, True]; x -= 1
for page_num in range(100):
if t[1] and t[0] < y:
t[0] = 1
print(page_num) # What to do after X times next Y times
elif t[1]:
t[1] = False
t[0] = 0
if not t[1] and t[0] < x:
t[0] = 1
elif not t[1]:
t[1] = True
t[0] = 0
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/464529.html
標籤:Python python-3.x 列表 算法 循环
下一篇:將6位儒略日期轉換為標準日期
