詳解Python中打亂列表順序random.shuffle()的使用方法
之前自己一直使用random中 randint生成隨機(jī)數(shù)以及使用for將列表中的數(shù)據(jù)遍歷一次。
現(xiàn)在有個需求需要將列表的次序打亂,或者也可以這樣理解:
【需求】將一個容器中的數(shù)據(jù)每次隨機(jī)逐個遍歷一遍。
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.
可選參數(shù)random是一個從0到參數(shù)的函數(shù),返回[0.0,1.0)中的隨機(jī)浮點;
如果random是缺省值None,則將使用標(biāo)準(zhǔn)的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):
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python操作MySQL數(shù)據(jù)庫的三種方法總結(jié)
下面小編就為大家分享一篇Python操作MySQL數(shù)據(jù)庫的三種方法總結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
如何在Windows環(huán)境下安裝PyMySQL(已安裝Anaconda)
這篇文章主要介紹了如何在Windows環(huán)境下安裝PyMySQL問題(已安裝Anaconda),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
利用python實現(xiàn)終身免費(fèi)的聽書工具
本文通過實際案例,詳細(xì)介紹了作者如何利用Python庫實現(xiàn)文本轉(zhuǎn)語音的過程,從安裝庫到實際操作案例,都有詳細(xì)的說明,為讀者提供了一定的參考價值2024-03-03

