sample(序列a,n) 功能:从序列a中随机抽取n个元素,并将n个元素生以list形式返回. 例: from random import randint, sample date = [randint(10,20) for _ in range(10)] c = sample(date, 5) print(c) 输出:[12, 17, 10, 12, 17] randint(10,20) for _ in range(10):从10~20间随机抽取10个数:…
最近一个月的时间,基本上都在加班加点的写业务,在写代码的时候,也遇到了一个有趣的问题,值得记录一下. 简单来说,需求是从一个字典(python dict)中随机选出K个满足条件的key.代码如下(python2.7): def choose_items(item_dict, K, filter): '''item_dict = {id:info} ''' candidate_ids = [id for id in item_dict if filter(item_dict[id])] if le…
#!/usr/bin/python ######################################### # Function: sample linux performance indices # Usage: python sampler.py # Author: CMS DEV TEAM # Company: Aliyun Inc. # Version: 1.1 ######################################### import os impor…