I am newbie in Python. I think I'm looking for something easy, but can't find. I have an numpy binary array, e.g.:
我是Python的新手。我想我正在寻找一些容易的东西,却找不到。我有一个numpy二进制数组,例如:
[1,0,1,1,0,0,0,1,1,1,1,0]
And I want to do 2 things:
我想做两件事:
-
Join (?) all elements into one number, so result will be:
将(?)所有元素加入一个数字,结果将是:
x=101100011110
-
Next want to converse it into binary, so:
接下来想把它转换成二进制,所以:
xx=2846
I have an algorithm to do 2., but I don't know how to do 1. I can do it using loop, but is it possible to do it using numpy, without loop? My array will be huge, so I need the best option.
我有一个算法做2.但我不知道该怎么做1.我可以使用循环来做,但是可以使用numpy,没有循环吗?我的阵列很大,所以我需要最好的选择。
3 个解决方案
#1
2
>>> int(''.join(map(str, [1,0,1,1,0,0,0,1,1,1,1,0])))
101100011110
Or with a little numpy:
或者有点numpy:
>>> int(''.join(np.array([1,0,1,1,0,0,0,1,1,1,1,0]).astype('|S1')))
101100011110
#2
1
I like @timgeb's answer, but if you're sure you want to use numpy calculations directly, you could do something like this:
我喜欢@ timgeb的答案,但是如果你确定要直接使用numpy计算,你可以这样做:
x = np.array([1,0,1,1,0,0,0,1,1,1,1,0])
exponents = np.arange(len(x))[::-1]
powers = 10**exponents
result = sum(powers * x)
In [12]: result
Out[12]: 101100011110
As pointed out by @Magellan88 in the comments, if you set powers=2**exponents
you can get from 0 to your second part of the question in one sweep.
正如@ Magellan88在评论中指出的那样,如果你设置powers = 2 **指数,你可以在一次扫描中从0到你问题的第二部分。
#3
0
Since you don't want loop in first task then you can go with map method , I just wanted to show you can also try this :
既然你不想在第一个任务中循环,那么你可以使用map方法,我只是想表明你也可以试试这个:
import numpy as np
array=np.array([1,0,1,1,0,0,0,1,1,1,1,0])
int_con=str(array).replace(',','').replace(' ','').replace('[','').replace(']','')
print("Joined {}".format(int_con))
bin_to_de=0
for digit in int_con:
bin_to_de=bin_to_de*2+int(digit)
print("Decimal conversion {}".format(bin_to_de))
output:
Joined 101100011110
Decimal conversion 2846
#1
2
>>> int(''.join(map(str, [1,0,1,1,0,0,0,1,1,1,1,0])))
101100011110
Or with a little numpy:
或者有点numpy:
>>> int(''.join(np.array([1,0,1,1,0,0,0,1,1,1,1,0]).astype('|S1')))
101100011110
#2
1
I like @timgeb's answer, but if you're sure you want to use numpy calculations directly, you could do something like this:
我喜欢@ timgeb的答案,但是如果你确定要直接使用numpy计算,你可以这样做:
x = np.array([1,0,1,1,0,0,0,1,1,1,1,0])
exponents = np.arange(len(x))[::-1]
powers = 10**exponents
result = sum(powers * x)
In [12]: result
Out[12]: 101100011110
As pointed out by @Magellan88 in the comments, if you set powers=2**exponents
you can get from 0 to your second part of the question in one sweep.
正如@ Magellan88在评论中指出的那样,如果你设置powers = 2 **指数,你可以在一次扫描中从0到你问题的第二部分。
#3
0
Since you don't want loop in first task then you can go with map method , I just wanted to show you can also try this :
既然你不想在第一个任务中循环,那么你可以使用map方法,我只是想表明你也可以试试这个:
import numpy as np
array=np.array([1,0,1,1,0,0,0,1,1,1,1,0])
int_con=str(array).replace(',','').replace(' ','').replace('[','').replace(']','')
print("Joined {}".format(int_con))
bin_to_de=0
for digit in int_con:
bin_to_de=bin_to_de*2+int(digit)
print("Decimal conversion {}".format(bin_to_de))
output:
Joined 101100011110
Decimal conversion 2846