Shuffling List Elements with Random.sample

  • Share this:

Code introduction


This function takes a list as input and then uses the sample method from the random module to shuffle the order of the elements in the list and return it.


Technology Stack : Random

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random

def shuffle_list(input_list):
    """
    随机打乱列表中的元素顺序。
    """
    return random.sample(input_list, len(input_list))                
              
Tags: