神剑山庄资源网 Design By www.hcban.com
之前自己一直使用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.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无详解Python中打乱列表顺序random.shuffle()的使用方法的评论...
更新日志
2024年09月30日
2024年09月30日
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】