Python random N lines from large file (no duplicate lines)

前端 未结 5 895
[愿得一人]
[愿得一人] 2020-12-11 23:42

I need to use python to take N number of lines from large txt file. These files are basically tab delimited tables. My task has the following constraints:

  • The
5条回答
  •  情书的邮戳
    2020-12-12 00:33

    • Obvious improvement would be to use set() for your usedPositions variable - lookup will be faster, and since you need to handle up to 10^6 used positions, lookup time is not irrelevant.
    • Use xrange instead of range in a for loop. Allocating full list of integers doesn't seem necessary.

提交回复
热议问题