Created
January 2, 2017 14:52
-
-
Save walsvid/89ae3d36a4f06bb336d491865e320a0c to your computer and use it in GitHub Desktop.
蓄水池抽样
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| import sys | |
| import random | |
| if len(sys.argv) == 3: | |
| input = open(sys.argv[2],'r') | |
| elif len(sys.argv) == 2: | |
| input = sys.stdin; | |
| else: | |
| sys.exit("Usage: python samplen.py <lines> <?file>") | |
| N = int(sys.argv[1]); | |
| sample = []; | |
| for i,line in enumerate(input): | |
| if i < N: | |
| sample.append(line) | |
| elif i >= N and random.random() < N/float(i+1): | |
| replace = random.randint(0,len(sample)-1) | |
| sample[replace] = line | |
| for line in sample: | |
| sys.stdout.write(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment