Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python shuffle() function

Python shuffle() function

Python Numbers Python Numbers


Description

shuffle() The method randomly shuffles all elements of a sequence.


Syntax

The syntax of the shuffle() method is as follows:



import random



random.shuffle (lst )

**Note:** shuffle() cannot be accessed directly; you need to import the random module and then call the method through the random static object.


Parameters


Return Value

This function has no return value.


Example

The following shows examples of using the shuffle() method:

Example

#!/usr/bin/python

-- coding: UTF-8 --

import

random

list

=

[

20

,

16

,

10

,

5

]

random

.

shuffle

(

list

)

print

"Randomly shuffled list: "

,

 

list

random

.

shuffle

(

list

)

print

"Randomly shuffled list: "

,

 

list

Running the example above produces:



随机排序列表 :  [16, 5, 10, 20]

随机排序列表 :  [16, 5, 20, 10]

Python Numbers Python Numbers