Python3 shuffle() Function

Python3 数字Python3 Numbers


Description

shuffle()The method randomly sorts all elements of the sequence.


Syntax

The following is the syntax of the shuffle() method:

import random random.shuffle (lst )

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


Parameters

  • lst -- A list.

Return Value

Returns None.


Example

The following shows an example of using the shuffle() method:

Example

#!/usr/bin/python3 import random list = [20, 16, 10, 5]; random.shuffle(list) print ("Randomly sort the list:", list) random.shuffle(list) print ("Randomly sort the list:", list)

The output after running the above example is:

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

Python3 数字Python3 Numbers

Other Extensions