zoukankan      html  css  js  c++  java
  • Python中shuffle函数

    shuffle() 方法将序列的所有元素随机排序。

    下面是语法:

    1 import random
    2 
    3 random.shuffle (lst )
    lst可以是序列或者元组;
     1 >>> import random;
     2 >>> indexList=[1,2,4,5,8,6];
     3 >>> indexList
     4 [1, 2, 4, 5, 8, 6]
     5 >>> random.shuffle(indexList)
     6 >>> indexList
     7 [1, 2, 4, 5, 8, 6]
     8 >>> random.shuffle(indexList)
     9 >>> indexList
    10 [6, 2, 4, 5, 1, 8]
    11 >>> random.shuffle(indexList)
    12 >>> indexList
    13 [2, 1, 4, 5, 6, 8]

     如果是'range'类型,则产生错误:

     1 >>> indexList=range(10);
     2 >>> type(indexList)
     3 <class 'range'>
     4 >>> random.shuffle(indexList)
     5 Traceback (most recent call last):
     6   File "<pyshell#28>", line 1, in <module>
     7     random.shuffle(indexList)
     8   File "D:softwareToolPythonanzhuang_filelib
    andom.py", line 272, in shuffle
     9     x[i], x[j] = x[j], x[i]
    10 TypeError: 'range' object does not support item assignment
  • 相关阅读:
    servlet程序开发
    jsp九大内置对象
    git原理教程
    jsp基础语法_Scriptlet_基本指令
    06_mysql多表查询
    05_mysql单表查询
    04_mysql增删改操作
    03_mysql索引的操作
    01_mysql数据库操作和基本数据类型
    生成器
  • 原文地址:https://www.cnblogs.com/yuzhuwei/p/4168314.html
Copyright © 2011-2022 走看看