2016-11-14 229 views
0

我試圖實現rsa算法。所以,我想結合基於塊大小的數組元素。例如:如何將數組元素轉換爲Python中的整數

blockSize = 2 
arr = [1, 3, 45, 6, 2, 90, 103] 

我願意以第一種和第二種方式合併爲一個元素的方式合併元素。所以數組是這樣的:

arr = [13, 456, 290, 103] 
+2

你嘗試過什麼嗎?什麼沒有奏效? – Jacobr365

+0

看看最高票數的答案[這裏](http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks)。從這個解決方案中,改變yield部分來代替它:'int(''。join(map(str,l [i:i + n])))' – idjaw

+0

'看看'int.from_bytes()'。 –

回答

2

鴻溝以塊由block_size,然後在每個小區內的所有值映射到string秒,加入字符串,並將其轉換爲integer

new_arr = [int(''.join(map(str, arr[i: i+block_size]))) for i in range(0, len(arr), block_size)] 

關於塊轉換的更詳細的概述:

int(''.join(map(str, arr[i: i+block_size]))) 
        arr[i: i+block_size]  for every chunk 
      map(str, ....................)  map every number in the chunks to string 
    ''.join(..............................) join these strings 
int(.......................................) convert the join string to integer 
+0

謝謝。它爲我工作。但我以另一種方式解釋它。像這樣: '對於索引範圍(0,len(arr),bs): new =''.join(map(str,arr [index:index + bs])) ' –

相關問題