import randomwith open('the_file','r') as source: data = [ (random.random(), line) for line in source ]data.sort()with open('another_file','w') as target: for _, line in data: target.write( line )那应该做。除非行数巨大(超过512个字符),否则300万行将适合大多数机器的内存。



