擅长:python、mysql、java
<p><strong>更新:</strong>在第3版中,位串模块的步进行为改变为更传统的含义。下面的答案是位串版本2。在</p>
<hr/>
<p>我是bitstring模块的作者,所以可能能够提供帮助(或者至少解释!)如前所述,<code>step</code>用于提供一个乘法器-如果您想以字节或其他比特倍数来指定内容,这将非常有用(因此<code>s[a:b:c]</code>与{<cd3>}相同)。在</p>
<p>对于你想要的行为,没有这么紧凑的语法。我很想知道你需要它做什么-我想不出它有什么好的用途,这就是为什么选择了step的另一个用途。我能想到的最好的对应物是</p>
<pre><code>s = ConstBitArray().join(mylist[i:i+1] for i in range(start, stop, step))
</code></pre>
<p>不过,这不太可能很快。在</p>
<p>如果您只想检查所有或任何位是否已设置/取消设置,那么您可以使用类似</p>
^{pr2}$
<p>或者</p>
<pre><code>mylist.any(False, range(start, stop, step))
</code></pre>
<hr/>
<p>另一种可能更快的方法是切换到使用<a href="http://pypi.python.org/pypi/bitarray" rel="nofollow">^{<cd4>}</a>模块进行切片:</p>
<pre><code>import bitarray
s = ConstBitArray(bitarray.bitarray(mylist)[start:stop:step])
</code></pre>
<p>如果您主要需要的只是这种切片行为,那么使用bitarray可能会更好,尽管它还不适用于python3。在</p>