Python中打乱列表顺序 random.shuffle()的使用方法

之前自己一直使用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):
  • 17
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值