Python shuffle() function

Python 数字Python 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 the method through the random static object.


Parameters

  • lst -- Can be a list.

Return Value

This function does not return a value.


Example

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

Example

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import random

list = [20, 16, 10, 5]
random.shuffle(list)
print "List after random shuffle : ",  list

random.shuffle(list)
print "List after random shuffle : ",  list

The output of the above example is:

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

Python 数字Python Numbers

Other Extensions