栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从列表中随机选择50个项目写入文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从列表中随机选择50个项目写入文件

如果列表是随机顺序的,则可以只取前50个。

否则,使用

import randomrandom.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)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/614144.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号