从列表中随机选择50项
我有一个函数,它可以从一个文件中读取一系列的项目。请问我怎么才能从这个列表中随机选出50个项目,然后写入另一个文件呢?
def randomizer(input, output='random.txt'):
query = open(input).read().split()
out_file = open(output, 'w')
random.shuffle(query)
for item in query:
out_file.write(item + '\n')
举个例子,如果我有一个包含所有随机项目的文件是这样的:
random_total = ['9', '2', '3', '1', '5', '6', '8', '7', '0', '4']
假设我想随机选出3个项目,结果可能是这样的:
random = ['9', '2', '3']
那么,我该如何从我随机化后的列表中选出50个呢?
更好的是,我怎么才能从原始列表中随机选出50个呢?
5 个回答
47
我觉得 random.choice()
是个更好的选择。
import numpy as np
mylist = [13,23,14,52,6,23]
np.random.choice(mylist, 3, replace=False)
这个函数会从列表中随机选出3个值,并把它们放在一个数组里。
52
选择随机项目的一个简单方法是先打乱顺序,然后再切割出你想要的部分。
import random
a = [1,2,3,4,5,6,7,8,9]
random.shuffle(a)
print a[:4] # prints 4 random variables
400
如果这个列表是乱七八糟的,你可以直接拿前50个。
否则,你可以使用
import random
random.sample(the_list, 50)
random.sample
的帮助说明:
sample(self, population, k) method of random.Random instance
Chooses k unique random elements from a population sequence.
Returns a new list containing elements from the population while
leaving the original population unchanged. The resulting list is
in selection order so that all sub-slices will also be valid random
samples. This allows raffle winners (the sample) to be partitioned
into grand prize and second place winners (the subslices).
Members of the population need not be hashable or unique. If the
population contains repeats, then each occurrence is a possible
selection in the sample.
To choose a sample in a range of integers, use xrange as an argument.
This is especially fast and space efficient for sampling from a
large population: sample(xrange(10000000), 60)