Python將二進制變為十進制
直接上代碼
def translate(pop): #數據轉換,二進制轉十進制
a = pop.dot(2 ** np.arange(10)[::-1])
print("二進制為"+str(pop))
print("轉化為十進制是:"+str(a))
pop = np.random.randint(2, size=(1, 10))
print(pop)
translate(pop)
- 首先使用numpy建立二進制數字。
- 利用dot點積進行十進制數字的計算