Home  >  Q&A  >  body text

Using an extremely long List in Python results in excessive memory usage

I need to use a List with a length of more than 500,000 in a program, but the result is that the memory usage increases instantly

What method should be used for optimization?

扔个三星炸死你扔个三星炸死你2634 days ago1405

reply all(1)I'll reply

  • 習慣沉默

    習慣沉默2017-07-05 10:53:29

    Does this list have to be stored in memory?

    If you are processing them one by one and do not need to store them all in memory, you can write the contents to a disk file first, then open it and use a statement similar to the following:

    for item in file('xxx.txt').xreadlines():
        ... # 解析出你的内容,并处理

    reply
    0
  • Cancelreply