1 回答

TA貢獻(xiàn)1790條經(jīng)驗 獲得超9個贊
您可以將 int 轉(zhuǎn)換為字符串,然后切片:
lst = [1010101010, 1111000110, 1111000111, 1111000101]
out = [lst[k:k+2] for k in range(0,len(lst), 2)]
result = [int(str(first)[:5] + str(second)[5:]) for first, second in out]
print(result)
輸出
[1010100110, 1111000101]
更新
如果輸入是 numpy 數(shù)組,則應(yīng)該這樣做:
import numpy as np
np.random.seed(42)
lst = np.random.randint(2, size=(50, 10))
out = [lst[k:k+2] for k in range(0,len(lst), 2)]
result = [int(''.join(map(str, first[:5].tolist() + second[5:].tolist()))) for first, second in out]
print(result)
輸出
[100001110, 1011101000, 11111000, 11101, 101011110, 111110110, 1010000000, 1011110101, 11110000, 10011100, 100101010, 11111101, 1010000110, 100010101, 1110111001, 1110001, 1011110011, 101001100, 1001011010, 1110011110, 1100100111, 1110111100, 101101100, 1010110101, 1101000101]
請注意,此解決方案通過將值轉(zhuǎn)換為 int 來刪除前導(dǎo)零。
添加回答
舉報