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


文本文件內容   ./txt

3
24
11
55
2
99
8
9
33
44

處理代碼:

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))

 輸出結果:

  bytearray(b'0011000011010000000000001000000001000000000010000000000100000000000000000000000000000000000000000001')
  161

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM