简体   繁体   中英

Shuffle a python list without using the built-in function

I'm working on writing two different shuffle functions.

The first shuffle function must take a list and return a new list with the elements shuffled into a random order.

This is what I have so far for the first shuffle function-

def shuf(List):
    import random
    newList=[]
    for i in List:
        i=random.randrange(len(List))
        newList+=i
    return newList

The second shuffle function takes a list as a parameter and shuffles the list in place.

I know how to do it with the built-in function but I'm not allowed to use it.

You might find that this implementation for shuffling suits your needs. Make sure that you note the difference between the two functions before using them.

import copy
import random


def main():
    my_list = list(range(10))
    print(my_list)
    print(shuffle(my_list))
    print(my_list)
    shuffle_in_place(my_list)
    print(my_list)


def shuffle(container):
    new_container = copy.copy(container)
    shuffle_in_place(new_container)
    return new_container


def shuffle_in_place(container):
    for index in range(len(container) - 1, 0, -1):
        other = random.randint(0, index)
        if other == index:
            continue
        container[index], container[other] = container[other], container[index]


if __name__ == '__main__':
    main()

Inspired by python source code implementation of shuffle in the random module

  import random

  def shuffle(A):
      last_index = len(A) - 1

      while last_index > 0:
          rand_index = random.randint(0, last_index)
          A[last_index], A[rand_index] = A[rand_index], A[last_index]
          last_index -= 1

      return A

You need to execute this code:

import random

#define the lists
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list2 = []
run = True

#main code
while run:
    var1 = random.choice(list1)
    var2 = list1.pop(var1)
    list2.append(var2)
    if len(list1) == 0:
        run = False
print (list2)
#output = [randomised list]

You can use this:

import random
def shuffle(lst):
     return sorted(lst ,key = lambda i:random.random())

Plan: Go through the list from the beginning starting with element 0; find a new random position for it, say 6, put 0's value in 6 and 6's value in 0. Move on to element 1 and repeat this process, and so on through the rest of the list

import random
iteration = random.randint(2, 100)
temp_var = 0
while iteration > 0:
    # We will be swapping the value of i for j.
    # And then setting j to what i was using the temp_var place holder.
    for i in range(1, len(my_list)): # have to use range with len()
        for j in range(1, len(my_list) - i):
            # Using temp_var as my place holder so I don't lose values
            temp_var = my_list[i]
            my_list[i] = my_list[j]
            my_list[j] = temp_var

        iteration -= 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM