简体   繁体   English

保存多个洗牌的 csv 文件

[英]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?有没有一种快速的方法可以将列表洗牌 n 次(以不同的顺序)并将其保存为 n 个单个 csv 文件? 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()

You could generate all possible permutations over the list indices, then pick the elements in the order given by the permutation to generate a new shuffled list.您可以在列表索引上生成所有可能的排列,然后按照排列给出的顺序选择元素以生成新的无序列表。 Finally, shuffle the list of lists and pick the first N.最后,打乱列表列表并选择前 N 个。

from itertools import permutations
from random import shuffle


example = ['S01_a', 'S01_b', 'S02_a', 'S02_b', 'S03_a', 'S03_b', 'S04_a']
indices = [x for x in range(0,len(example))]
n_perm = 5

all_permutations = list(set(permutations(indices)))
shuffle(all_permutations)
my_permutations = all_permutations[:n_perm]       

for index, elem in enumerate(my_permutations):

    new_shuffle = [example[x] for x in elem]    
    with open("example_{}.csv".format(str(index)), "w") as fid:
        fid.writelines(",".join(new_shuffle))

You can do:你可以做:

import itertools, random
N = 5
example = ['S01_a', 'S01_b', 'S02_a', 'S02_b', 'S03_a', 'S03_b', 'S04_a']
all_options = list(itertools.permutations(example, len(example)))
my_lists = random.choices(all_options, k=N)
my_lists

Output:输出:

[('S02_b', 'S01_a', 'S03_a', 'S01_b', 'S02_a', 'S03_b', 'S04_a'),
 ('S03_b', 'S02_b', 'S01_b', 'S03_a', 'S01_a', 'S02_a', 'S04_a'),
 ('S02_a', 'S04_a', 'S03_a', 'S02_b', 'S03_b', 'S01_b', 'S01_a'),
 ('S02_b', 'S04_a', 'S01_a', 'S02_a', 'S03_b', 'S01_b', 'S03_a'),
 ('S03_a', 'S04_a', 'S01_b', 'S02_a', 'S02_b', 'S01_a', 'S03_b')]

And then if you want to do something for each of them seperatly, just loop them:然后,如果您想分别为每个人做某事,只需循环它们:

for l in my_lists:
    I_do_what_it_want with l...

something like this ?像这样的东西? you have to looking for a library to genrate csv你必须寻找一个库来生成 csv

import java.util.*; 

public class GFG 
{ 
    public static void main(String[] args) 
    { 
        ArrayList<String>  mylist = new ArrayList<String>(); 
        mylist.add("code"); 
        mylist.add("quiz"); 
        mylist.add("geeksforgeeks"); 
        mylist.add("quiz"); 
        mylist.add("practice"); 
        mylist.add("qa"); 

        System.out.println("Original List : \n" + mylist); 

        // Here we use Random() to shuffle given list. 
        Collections.shuffle(mylist, new Random()); 
        System.out.println("\nShuffled List with Random() : \n"
                                                     + mylist); 

        // Here we use Random(3) to shuffle given list. 
        Collections.shuffle(mylist, new Random(3)); 
        System.out.println("\nShuffled List with Random(3) : \n"
                                                     + mylist); 

        // Here we use Random(3) to shuffle given list. 
        Collections.shuffle(mylist, new Random(5)); 
        System.out.println("\nShuffled List with Random(5) : \n"
                                                     + mylist); 

    } 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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