[英]How to add large number of items to list without getting memory error?
我正在尝试将大量(块)项目添加到列表中。 列表最多只能包含 536,870,912 项。 我有 10 亿行代码的数据集,我收到 memory 错误,因为我正在尝试将 append 10 亿项添加到列表中。
list = ["1","2","3","4",... 1 billion]
window_size = 4
db_chunk_hash = []
for i in range(len(list) - window_size + 1):
c = list[i: i + window_size]
i = bytes(str(c), encoding = 'utf8')
db_chunk_hash.append(i)
如何解决这个问题呢?
您可以使用 Python 生成器 function,这样您只在需要时使用数据,这是 memory 上更好的方法。
def chunks(lst, window_size):
for i in range(len(lst) - window_size + 1):
c = lst[i: i + window_size]
yield bytes(str(c), encoding='utf8')
window_size = 4
db_chunk_hash = chunks(list_t, window_size)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.