Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Randomly mix lines of 3 million-line file

Tags:

python

vim

random

Everything is in the title. I'm wondering if any one knows a quick and with reasonable memory demands way of randomly mixing all the lines of a 3 million lines file. I guess it is not possible with a simple vim command, so any simple script using Python. I tried with python by using a random number generator, but did not manage to find a simple way out.

like image 210
Nigu Avatar asked Jan 06 '11 18:01

Nigu


People also ask

How do you shuffle a line in a text file in Linux?

Using the shuf Command The shuf utility is a member of the GNU Coreutils package. It outputs a random permutation of the input lines. The shuf command will load all input data into memory during the shuffling, and it won't work if the input file is larger than the free memory.

How do you shuffle a list in Python?

To shuffle strings or tuples, use random. sample() , which creates a new object. random. sample() returns a list even when a string or tuple is specified to the first argument, so it is necessary to convert it to a string or tuple.

What is random shuffle in Python?

Python Random shuffle() Method The shuffle() method takes a sequence, like a list, and reorganize the order of the items. Note: This method changes the original list, it does not return a new list.


1 Answers

Takes only a few seconds in Python:

import random lines = open('3mil.txt').readlines() random.shuffle(lines) open('3mil.txt', 'w').writelines(lines) 
like image 106
John Kugelman Avatar answered Sep 20 '22 04:09

John Kugelman