遍历数字并评估数字是否可被某些数字整除 [英] loop through numbers and evaluate if numbers are divisible by certain numbers
问题描述
我写了一个程序来评估某个范围内的哪些数字只能被某些数字(在 1
到 9
范围内)整除.到目前为止,该代码似乎仍然有效,但是我在pythontutor http://www.pythontutor.com/visualize.html#mode=edit ,发生了一些奇怪的事情.
i wrote a program to evaluate what numbers in a certain range are divisble by only certain numbers (in the range 1
to 9
). So far the code seems to work but i tested the steps it takes through at pythontutor http://www.pythontutor.com/visualize.html#mode=edit and something odd happened.
在第二个循环中,代码并不总是检查所有值(k)
的可除性,但有时会忽略最后一个值(k)
.最好给你一个例子:
In the second loop, the code doesnt always check all values (k)
for its divisibility but sometimes leaves out the last value (k)
.
Better to give you an example:
Liste = []
for i in range (1500, 1700):
if i%5 == 0 and i%7 == 0:
Liste.append(i)
print (Liste)
Teiler = [2, 3, 4, 6, 8, 9]
for k in Liste:
z = 0
for w in range (len(Teiler)):
if k%Teiler[z] == 0:
Liste.remove(k)
z += 1
break
else:
z += 1
print (Liste)
这里的输出如下:
[1505, 1540, 1575, 1610, 1645, 1680]
[1505, 1575, 1645]
其输出(如第二个列表所示)应该是被 5
和 7
整除的数字.同样在pythontutor上,第二个for循环遍历所有值(k)
.
And its the output that its supposed to be, as in the second list are only the numbers divisible by 5
and 7
. Also on pythontutor the second for loop goes through all values (k)
.
但是当我像下面那样更改数字范围时(第2行,将范围从 1700
更改为 1800
):
But when i change the range of number like the following (line 2, change range from 1700
to 1800
):
Liste = []
for i in range (1500, 1800):
if i%5 == 0 and i%7 == 0:
Liste.append(i)
print (Liste)
Teiler = [2, 3, 4, 6, 8, 9]
for k in Liste:
z = 0
for w in range (len(Teiler)):
if k%Teiler[z] == 0:
Liste.remove(k)
z += 1
break
else:
z += 1
print (Liste)
输出如下:
[1505, 1540, 1575, 1610, 1645, 1680, 1715, 1750, 1785]
[1505, 1575, 1645, 1715, 1785]
如果我在pythontutor上运行此代码,则代码在 k = 1750
处停止,并且不检查 k = 1785
.
If i run this code on pythontutor, the code stops at k = 1750
and doesnt check for k = 1785
.
为什么会这样?检查pythontutor是否有问题?还是我的代码有问题?
Why is it behaving that way? Is it a problem with the check on pythontutor? Or is there a problem with my code?
我想了解Python为什么要这样做.
I want to understand why Python is doing what it does.
非常感谢您的帮助.我很抱歉,如果这是一个菜鸟问题,或者我错过了明显的东西.
Thank you very much for your help. Iam sorry if this is a noob question or iam missing out something obvious.
推荐答案
请勿从您迭代过的列表中删除项目,因为其他答案都已说明.更改列表的长度会影响迭代.示例:
Don't remove items from the list you iterate over, as the other answer states. Changing the length of the list affects the iteration. Example:
>>> L=list(range(10))
>>> for i in L:
... if i==5: L.remove(i)
... print(i)
...
0
1
2
3
4
5 # removed 5, and skipped 6!
7
8
9
也不会使用 w
,尽管 z
是等效的.而是直接在 Teiler
上进行迭代,并在满足条件的情况下将项目添加到新列表中:
Also w
is never used, although z
is the equivalent. Instead, iterate directly over Teiler
and add items to a new list if it satisfies your condition:
Liste = []
for i in range (1500, 1800):
if i%5 == 0 and i%7 == 0:
Liste.append(i)
print (Liste)
Liste2 = []
Teiler = [2, 3, 4, 6, 8, 9]
for k in Liste:
for w in Teiler:
if k % w == 0:
break
else:
Liste2.append(k)
print(Liste2)
输出:
[1505, 1645, 1715]
如果您没有看到 for/else
,则只有在不中断 for
循环的情况下才会执行 else
,因此 k%w!= 0
的所有值都必须为true.
If you haven't seen for/else
, the else
only executes if you don't break from the for
loop, so all of k % w != 0
has to be true.
另一种选择是使用列表理解,真正简化了代码:
Another option is to use list comprehensions, which really simplify the code:
L = [i for i in range(1500,1800) if i%5 == 0 and i%7 == 0]
L = [x for x in L if all(x % k != 0 for k in (2,3,4,6,8,9))]
print(L)
输出:
[1505, 1645, 1715]
注释1575和1785可被3整除,因此您的两个样本列表都因删除列表中的值而出错.
Note 1575 and 1785 are divisible by 3, so both your sample lists had errors from removing values in the list.
这篇关于遍历数字并评估数字是否可被某些数字整除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!