python 實現文本文件中的數字按序排序(位操做,低內存佔用)

文本文件內容   ./txtpython

3
24
11
55
2
99
8
9
33
44app

處理代碼:blog

import sys
a = bytearray(b'')
for i in range(100):
    a.append(ord('0'))
f = open('./txt','r')
line = f.readline()
while line:
    try:
        index = int(line)
        a[index] = ord('1')
        line = f.readline()
    except Exception as e:
        pass
f.close()
print(a)
print(sys.getsizeof(a))

 輸出結果:get

  bytearray(b'0011000011010000000000001000000001000000000010000000000100000000000000000000000000000000000000000001')
  161io

相關文章
相關標籤/搜索