Python:我如何在步骤“中有选择地切换布尔元素?”;";在阵列中?
我在一条线上放了50个圣诞灯,当它们第一次插上电源时,所有的灯都熄灭了。灯光由一个按钮控制,每次按下按钮时,一些灯光都会翻转其状态(即,如果关闭,则变为打开;如果打开,则变为关闭)。灯的选择取决于迄今为止按下按钮的次数:Python:我如何在步骤“中有选择地切换布尔元素?”;";在阵列中?,python,arrays,boolean,toggle,Python,Arrays,Boolean,Toggle,我在一条线上放了50个圣诞灯,当它们第一次插上电源时,所有的灯都熄灭了。灯光由一个按钮控制,每次按下按钮时,一些灯光都会翻转其状态(即,如果关闭,则变为打开;如果打开,则变为关闭)。灯的选择取决于迄今为止按下按钮的次数: 在第1次按下时,第1、2、3、4、…、50个灯的状态会发生变化 在第二次按下时,第二、第四、第六,…,灯的状态发生变化 在第三次按下时,第三、第六、第九,…,指示灯改变状态 等等 在第n次按下时,与n的所有倍数对应的灯光将改变状态 在任何给定数量的“n”次按下后,我如何找出
- 在第1次按下时,第1、2、3、4、…、50个灯的状态会发生变化
- 在第二次按下时,第二、第四、第六,…,灯的状态发生变化李>
- 在第三次按下时,第三、第六、第九,…,指示灯改变状态李>
- 等等
- 在第n次按下时,与n的所有倍数对应的灯光将改变状态
lightsarray = [False] * 50 ### All lights are off at start ###
def pressbutton(n):
count = 0
for count in range(0, n):
lightsarray[::int(n)] = [not y for y in lightsarray[::int(n)]]
count = count + 1
return lightsarray
目前,n=1的输出为True,True,True,True,True,True…
n=2的输出是True,False,True,False,True,False…
到目前为止还不错。但是对于n=3,我期望True,False,False,False,True,True,True…
但是我得到了真,假,假,真,假,假…
我想我在分配/获取数组状态时出错了。我如何纠正这个问题?还是有更好的方法
--编辑--
谢谢大家的回复。我实现了这些更改,并得到了您建议的输出,但我不确定这是否符合灯光的预期状态。记住False=OFF和True=ON,让我们看看前5个灯光。这就是我们所期望的:
>>> pressbutton(1)
[True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True]
>>> pressbutton(2)
[False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True]
>>> pressbutton(3)
[True, True, False, False, False, True, True, True, False, False, False, True, True, True, False, False, False, True, True, True, False, False, False, True, True, True, False, False, False, True, True, True, False, False, False, True, True, True, False, False, False, True, True, True, False, False, False, True, True, True]
对于0次按压,所有操作都处于关闭状态:
False, False, False, False, False...
对于1次按下,每按1的倍数,灯光将翻转:
True, True, True, True, True...
True, False, True, False, True...
True, False, False, False, True...
对于2次按压,每按2次的倍数翻转一次灯光:
True, True, True, True, True...
True, False, True, False, True...
True, False, False, False, True...
请注意,第二个和第四个灯光已切换,否则其余灯光保持不变
对于3次按压,每按3次的倍数翻转一次灯光:
True, True, True, True, True...
True, False, True, False, True...
True, False, False, False, True...
但是,在进行更改后,例如,2次按给出False,True…
而不是True,False…
,3次按给出True,True,False,False…
(开头有两个True)
我如何纠正这个问题?我希望这是有意义的。如果我理解这个过程,我会说,对于每个步骤k,我们首先选择第k个元素,然后移动到第2个元素,3k,4k 这可以通过使用带有步长参数的xrange(我更喜欢xrange而不是range,因为它不会填满内存)来实现 这里有一个这样进步的例子
for k in xrange(1, n + 1):
print range(k, n, k)
这应该可以做到,这是输出,看起来与您的示例类似:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 4, 6, 8]
[3, 6, 9]
[4, 8]
[5]
[6]
[7]
[8]
[9]
[]
不是因为你不必去n+1,你可以停在n:)如果我理解这个过程,我会说对于每个步骤k,我们首先选择第k个元素,然后移动到第2个元素,3k,4k 这可以通过使用带有步长参数的xrange(我更喜欢xrange而不是range,因为它不会填满内存)来实现 这里有一个这样进步的例子
for k in xrange(1, n + 1):
print range(k, n, k)
这应该可以做到,这是输出,看起来与您的示例类似:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 4, 6, 8]
[3, 6, 9]
[4, 8]
[5]
[6]
[7]
[8]
[9]
[]
尽管您不必转到n+1,但您可以在n:)停止。您的主要问题是在同一个函数中以多种方式使用count。本质上,范围内计数的
使用与count=0
相同的变量,并且count=count+1
正在修改循环变量
另一个问题是,每次运行函数时都要修改全局lightsarray
,因此输出将取决于之前对函数进行rand的方式
最后,如果您的pressbutton
功能应该显示按下按钮n
次的结果,就像它看起来所做的那样,您应该在每个循环迭代中改变不同的灯光,而不是在每个循环迭代中翻转每个n
次灯光
你应该做些什么
def pressbutton(n):
lightsarray = [False] * 50 ### All lights are off at start ###
for count in range(0, n):
lightsarray[::count] = [not y for y in lightsarray[::count]]
return lightsarray
您的主要问题是在同一个函数中以多种方式使用count。本质上,范围内计数的使用与count=0
相同的变量,并且count=count+1
正在修改循环变量
另一个问题是,每次运行函数时都要修改全局lightsarray
,因此输出将取决于之前对函数进行rand的方式
最后,如果您的pressbutton
功能应该显示按下按钮n
次的结果,就像它看起来所做的那样,您应该在每个循环迭代中改变不同的灯光,而不是在每个循环迭代中翻转每个n
次灯光
你应该做些什么
def pressbutton(n):
lightsarray = [False] * 50 ### All lights are off at start ###
for count in range(0, n):
lightsarray[::count] = [not y for y in lightsarray[::count]]
return lightsarray
你换错了灯;您使用的是循环计数器n
,而不是count
,因此您总是切换错误的指示灯
您需要从1开始计数,然后计数到n
(因此范围为'n+1)
你的压痕是错误的;你回来太早了。取消使return
语句不属于循环的一部分
我还将使lightsarray
值成为函数本身的一部分,这里不要使用全局值,因为每次调用函数时它都会发生变化,这使得测试函数本身变得更加困难
最后但并非最不重要的一点是,无需手动递增count
,这就是for
循环已经在为您做的事情:
def pressbutton(n):
lightsarray = [False] * 50 ### All lights are off at start ###
for count in range(1, n + 1):
lightsarray[::count] = [not y for y in lightsarray[::count]]
return lightsarray
现在,这项工作如预期的那样:
>>> pressbutton(1)
[True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True]
>>> pressbutton(2)
[False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True]
>>> pressbutton(3)
[True, True, False, False, False, True, True, True, False, False, False, True, True, True, False, False, False, True, True, True, False, False, False, True, True, True, False, False, False, True, True, True, False, False, False, True, True, True, False, False, False, True, True, True, False, False, False, True, True, True]
你换错了灯;您使用的是循环计数器n
,而不是count
,因此您总是切换错误的指示灯
您需要从1开始计数,然后计数到n
(因此范围为'n+1)
你的契约