温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在Python中使用random.shuffle()函数打乱列表顺序

发布时间:2021-03-23 15:39:27 来源:亿速云 阅读:311 作者:Leah 栏目:开发技术

怎么在Python中使用random.shuffle()函数打乱列表顺序?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

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):

关于怎么在Python中使用random.shuffle()函数打乱列表顺序问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI