Python/Numpy:将布尔列表转换为unsigned int [英] Python/Numpy: Convert list of bools to unsigned int
问题描述
-
最快(或最"Pythonic")转换方式是什么
What is the fastest (or most "Pythonic") way to convert
x = [False, False, True, True]
变成12
? (如果有这种方法.)
into 12
? (If there is such a way.)
如果x
是bool的numpy.array
怎么办?有特殊的命令吗?
What if x
were instead a numpy.array
of bools? Is there a special command for that?
我有一个大型的n×n布尔数组,其中每个n元素行代表一个高维特征向量的单个低维哈希. (在上面的示例中,n =4.)我想知道答案,以便尽可能地压缩我的数据.谢谢.
I have a large m-by-n array of booleans, where each n-element row represents a single low-dimensional hash of a high-dimensional feature vector. (In the example above, n = 4.) I would like to know the answer in order to compress my data as much as possible. Thank you.
编辑:谢谢您的答复!使用以下测试代码
Edit: Thank you for the responses! Using the following test code,
t = 0
for iter in range(500):
B = scipy.signbit(scipy.randn(1000,20))
for b in B:
t0 = time.clock()
# test code here
t1 = time.clock()
t += (t1-t0)
print t
...这是我的Thinkpad笔记本电脑上的运行时:
...here were the runtimes on my Thinkpad laptop:
- 我的答案: 4.26秒
- Sven Marnach 1 :7.88
- Emil H : 8.51
- Sven Marnach 2 :8.72
- delnan :10.14
- liori :53.49
- My answer: 4.26 sec
- Sven Marnach 1: 7.88
- Emil H: 8.51
- Sven Marnach 2: 8.72
- delnan: 10.14
- liori: 53.49
当然,我欢迎任何可能确认或反驳我的数据的独立测试!
Of course, I welcome any independent tests that may confirm or refute my data!
编辑:在下面的回答中,将int(j)
更改为简单的j
仍然可行,但运行速度慢了六倍!然后,如果使用int
投掷布尔值,则其他答案可能会变得更快.但是我太懒了,无法再次测试所有内容.
Edit: In my answer below, changing int(j)
to simply j
still works, but runs six times as slow! Then perhaps the other answers would become faster if the bool was casted using int
. But I'm too lazy to test everything again.
编辑:liori在此处发布了独立测试的结果.
Edit: liori posted results of independent tests here.
推荐答案
从各种其他答案中得出各种想法,这是另一种方法:
Taking various ideas from various other answers, here's another way to do it:
sum(1<<i for i, b in enumerate(x) if b)
在我的测试中,这是非常快的-即使它像疯了一样溢出,也要使用numpy方法处理大量位.我使用liori的测试模块进行测试.我建议的更改使Steve的方法快了一点.但是,如果一次需要进行很多这类转换(且位数不太多),我敢打赌numpy会更快.
It is quite fast in my tests - right up with the numpy method for large number of bits even though it overflows like crazy. I used liori's testing module for testing. Steve's method, with the change I suggested, is just barely faster. However, if a lot of these sorts of conversions need to be done at a time (and with not too many bits), I'm betting that numpy will be faster.
这篇关于Python/Numpy:将布尔列表转换为unsigned int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!