之前自己一直使用random中 randint生成随机数以及使用for将列表中的数据遍历一次。
现在有个需求需要将列表的次序打乱,或者也可以这样理解:
【需求】将一个容器中的数据每次随机逐个遍历一遍。
random.shuffle()方法提供了完美的解决方案。
不会生成新的列表,只是将原列表的次序打乱
# shuffle()使用样例 import random x = [i for i in range(10)] print(x) random.shuffle(x) print(x)
源码及注释(个人翻译注释)
def shuffle(self, x, random=None): """Shuffle list x in place, and return None. 原位打乱列表,不生成新的列表。 Optional argument random is a 0-argument function returning a random float in [0.0, 1.0); if it is the default None, the standard random.random will be used. 可选参数random是一个从0到参数的函数,返回[0.0,1.0)中的随机浮点; 如果random是缺省值None,则将使用标准的random.random()。 """ if random is None: randbelow = self._randbelow for i in reversed(range(1, len(x))): # pick an element in x[:i+1] with which to exchange x[i] j = randbelow(i + 1) x[i], x[j] = x[j], x[i] else: _int = int for i in reversed(range(1, len(x))): # pick an element in x[:i+1] with which to exchange x[i] j = _int(random() * (i + 1)) x[i], x[j] = x[j], x[i]
random 中其他的方法
class Random(_random.Random): ## -------------------- integer methods ------------------- def randrange(self, start, stop=None, step=1, _int=int): def randint(self, a, b): def _randbelow(self, n, int=int, maxsize=1 << BPF, type=type, Method=_MethodType, BuiltinMethod=_BuiltinMethodType): ## -------------------- sequence methods ------------------- def choice(self, seq): def shuffle(self, x, random=None): def sample(self, population, k): def choices(self, population, weights=None, *, cum_weights=None, k=1): ## -------------------- uniform distribution ------------------- def uniform(self, a, b): ## -------------------- triangular -------------------- def triangular(self, low=0.0, high=1.0, mode=None): ## -------------------- normal distribution -------------------- def normalvariate(self, mu, sigma): ## -------------------- lognormal distribution -------------------- def lognormvariate(self, mu, sigma): ## -------------------- exponential distribution -------------------- def expovariate(self, lambd): ## -------------------- von Mises distribution -------------------- def vonmisesvariate(self, mu, kappa): ## -------------------- gamma distribution -------------------- def gammavariate(self, alpha, beta): ## -------------------- Gauss (faster alternative) -------------------- def gauss(self, mu, sigma): def betavariate(self, alpha, beta): ## -------------------- Pareto -------------------- def paretovariate(self, alpha): ## -------------------- Weibull -------------------- def weibullvariate(self, alpha, beta):
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年09月24日
2024年09月24日
- 国风大师纯音系列《民乐琴王童丽-与古筝的约会》1CD[MP3][166.4MB]
- 刘美君.1993-被你纵坏【SONY】【WAV+CUE】
- 黄鹤翔.1998-爱的咏叹调【中唱】【WAV+CUE】
- 薰妮.1983-梦+淡(2000年永恒真存双碟版)【永恒】【WAV+CUE】
- 方伊琪.1976-时光消逝(2015复黑限量版)【风行】【WAV+CUE】
- 群星.1990-宝丽金影视金曲巡礼【宝丽金】【WAV+CUE】
- 林俊杰.2014-新地球(精装预售“光”版)【华纳】【WAV+CUE】
- 国风大师纯音系列《王珣学院派独特的空灵古筝》1CD[FLAC][888MB]
- 国风大师纯音系列《古筝演奏家 付娜》1CD[FLAC][1.2GB]
- 国风大师纯音系列《古筝仙子 常静演奏 十指弹一》1CD[FLAC][919MB]
- DasKapital-OneMustHaveChaosInsidetoGiveBirthtoaDancingStar(2024)[24-44.1]WAV
- 王菲.1999-《只爱陌生人》日本东芝版[低速原抓WAV+CUE]
- 张小英1987-金奖金曲第一集[新加坡版][WAV+CUE]
- 鹿晗.2015-重启【海蝶】【FLAC分轨】
- 张芸京.2012-小女孩【金牌大风】【WAV+CUE】