gam*_*ter 0 python loops sequence modulo
使用% 2给了我交替序列[0, 1, 0, 1, ...]
seq = []
for i in range(10):
e = i % 2
seq.append(e)
Run Code Online (Sandbox Code Playgroud)
有没有办法通过从循环内部[0, 0, 1, 1, 0, 0, 1, 1,...]生成元素来生成序列?
seq = []
for i in range(10):
e = the_solution(i)
seq.append(e)
Run Code Online (Sandbox Code Playgroud)
听起来您想放慢现有序列的速度,从而有效地将每个元素的长度加倍。也就是说,新序列的索引 0 和 1 应与旧序列的索引 0 相对应;新序列的 2 和 3 应对应于旧序列的索引 1;等等。我们可以使用整数除法来获得这种行为。
e = (i // 2) % 2
Run Code Online (Sandbox Code Playgroud)
//类似于/,但它向下舍入到下一个整数,因此2 // 2和3 // 2都是1。
| 归档时间: |
|
| 查看次数: |
626 次 |
| 最近记录: |