lundi 25 novembre 2019

Save multiple shuffled csv files

is there a quick way to shuffle a list n times (in a different order) and save it as n single csv file? I already searched a lot, but couln't find anything about that. I have the following code, but I'm sure it could be shorter and with this I can't be sure that all shuffled lists have a different order. Has someone a solution?

import random

example = ['S01_a', 'S01_b', 'S02_a', 'S02_b', 'S03_a', 'S03_b', 'S04_a']

while True:
    example

    shuffle3 = []

    last = ""

    while example:
        i = example
        if last:
            i = [x for x in i if x[4] != last[4]]
        if not i:
            #no valid solution
            break 
        newEl = random.choice(i)
        last = newEl
        shuffle3.append(newEl)
        example.remove(newEl)
    if not example:
        break


fid = open("example.csv", encoding='latin1', mode="w")
fid.writelines(shuffle3)
fid.close()



Aucun commentaire:

Enregistrer un commentaire