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

Python3 shuffle() Function

Python3 shuffle() Function

Python3 Numbers Python3 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 call the method through the static random object.


Parameters


Return Value

Returns None.


Example

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

Example

#!/usr/bin/python3

import

random

list

=

[

20

,

16

,

10

,

5

]

;

random

.

shuffle

(

list

)

print

(

"

Randomly sorted list:

"

,

list

)

random

.

shuffle

(

list

)

print

(

"

Randomly sorted list:

"

,

list

)

Running the example above produces:


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

Python3 Numbers Python3 Numbers