欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Python中打亂列表順序random.shuffle()的使用方法

 更新時(shí)間:2019年11月11日 10:57:45   作者:chichu261  
這篇文章主要介紹了詳解Python中打亂列表順序random.shuffle()的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

之前自己一直使用random中 randint生成隨機(jī)數(shù)以及使用for將列表中的數(shù)據(jù)遍歷一次。

現(xiàn)在有個(gè)需求需要將列表的次序打亂,或者也可以這樣理解:

【需求】將一個(gè)容器中的數(shù)據(jù)每次隨機(jī)逐個(gè)遍歷一遍。

random.shuffle()方法提供了完美的解決方案。

不會(huì)生成新的列表,只是將原列表的次序打亂

# shuffle()使用樣例
import random

x = [i for i in range(10)]
print(x)
random.shuffle(x)
print(x)

源碼及注釋(個(gè)人翻譯注釋)

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是一個(gè)從0到參數(shù)的函數(shù),返回[0.0,1.0)中的隨機(jī)浮點(diǎn);
 如果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):

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • pytest用例間參數(shù)傳遞的兩種實(shí)現(xiàn)方式示例

    pytest用例間參數(shù)傳遞的兩種實(shí)現(xiàn)方式示例

    pytest提供了許多運(yùn)行命令以供定制化運(yùn)行某一類測(cè)試用例或者某個(gè)測(cè)試用例等,下面這篇文章主要給大家介紹了關(guān)于pytest用例間參數(shù)傳遞的兩種實(shí)現(xiàn)方式,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-12-12
  • python按照多個(gè)條件排序的方法

    python按照多個(gè)條件排序的方法

    今天小編就為大家分享一篇python按照多個(gè)條件排序的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-02-02
  • python實(shí)現(xiàn)requests發(fā)送/上傳多個(gè)文件的示例

    python實(shí)現(xiàn)requests發(fā)送/上傳多個(gè)文件的示例

    今天小編就為大家分享一篇python實(shí)現(xiàn)requests發(fā)送/上傳多個(gè)文件的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • python保存字典和讀取字典的實(shí)例代碼

    python保存字典和讀取字典的實(shí)例代碼

    這篇文章主要介紹了python保存字典和讀取字典的實(shí)例代碼,通過(guò)代碼給大家介紹了python 使用列表和字典存儲(chǔ)信息的相關(guān)代碼,需要的朋友可以參考下
    2019-07-07
  • pandas創(chuàng)建DataFrame的7種方法小結(jié)

    pandas創(chuàng)建DataFrame的7種方法小結(jié)

    這篇文章主要介紹了pandas創(chuàng)建DataFrame的7種方法小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • pandas object格式轉(zhuǎn)float64格式的方法

    pandas object格式轉(zhuǎn)float64格式的方法

    下面小編就為大家分享一篇pandas object格式轉(zhuǎn)float64格式的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • python實(shí)現(xiàn)微信跳一跳輔助工具步驟詳解

    python實(shí)現(xiàn)微信跳一跳輔助工具步驟詳解

    這篇文章主要介紹了python實(shí)現(xiàn)微信跳一跳輔助工具的步驟詳解以及使用說(shuō)明,需要的朋友可以參考下
    2018-01-01
  • Python and、or以及and-or語(yǔ)法總結(jié)

    Python and、or以及and-or語(yǔ)法總結(jié)

    這篇文章主要介紹了Python and、or以及and-or語(yǔ)法總結(jié),本文分別給出實(shí)例講解它們的使用方法,需要的朋友可以參考下
    2015-04-04
  • python網(wǎng)絡(luò)爬蟲基于selenium爬取斗魚直播信息

    python網(wǎng)絡(luò)爬蟲基于selenium爬取斗魚直播信息

    目前是直播行業(yè)的一個(gè)爆發(fā)期,由于國(guó)家對(duì)直播行業(yè)進(jìn)行整頓和規(guī)范,現(xiàn)在整個(gè)直播行業(yè)也在穩(wěn)固發(fā)展。隨著互聯(lián)網(wǎng)和網(wǎng)絡(luò)直播市場(chǎng)的快速發(fā)展,相信未來(lái)還有廣闊的發(fā)展前景。今天用selenium爬取一下斗魚直播信息將代碼分享給大家
    2022-03-03
  • 詳解Python中for循環(huán)是如何工作的

    詳解Python中for循環(huán)是如何工作的

    如果你對(duì)python中的for循環(huán)不是很清楚,那么建議你看看這篇文章,本文主要給大家介紹了關(guān)于Python中for循環(huán)是如何工作的相關(guān)資料,介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-06-06

最新評(píng)論