0
list1 = [1,2,3,4,5]
list2 = [2,3,4,1,2]
list3 = [2,4,6,2,1]
def random_chunk(li, min_chunk=1, max_chunk= 3):
     it = iter(li)
     while True:
        nxt = list(islice(it,randint(min_chunk,max_chunk)))
        if nxt:
            yield nxt
        else:
            break

def shuffle(a, b, c):
    assert len(a) == len(b) == len(c)
    start_state = random.getstate()
    random.shuffle(a)
    random.setstate(start_state)
    random.shuffle(b)
    random.setstate(start_state)
    random.shuffle(c)
    random.setstate(start_state)

shuffle(list1, list2, list3)
slice = list(random_chunk(list1))

After mixing the lists randomly, I ended up coding randomly using the chunk function. But it is not easy to want the two lists to be equally divided. How can I cut multiple lists the same way? For example, when list1 = [1,2,3,4,5] is cut into [1,2], [3,4,5], list2 is also [2,3], [4,2,1 ].I really appreciate it if you let me know.

  • 2
    You're generating a random number *when you make the chunks*. This means that every time you call that function, you're going to get random chunks. How about, instead, you get the random number *before* starting to chunk the lists, and then pass it in like that? Or record the seed that the random number generator is on before starting to slice each list, and make sure each list starts at the same seed? – Green Cloak Guy Nov 18 '18 at 18:00

2 Answers2

1

The zip functions allows you to combine, decorate, the lists. Then, simply choose your chunks and separate them (undecorate):

import random

list1 = [1,2,3,4,5]
list2 = [2,3,4,1,2]
list3 = [2,4,6,2,1]

lists = [list1, list2, list3]


pack = list(zip(*lists))
print(pack)

random.seed(0)
random.shuffle(pack)

print(pack[2:4])
chunk1, chunk2, chunk3 = zip(*pack[2:4])

print(chunk1, chunk2, chunk3)
Vitor SRG
  • 664
  • 8
  • 9
  • Thank you for answer. I think you understood my question a little differently. What I want is that when list1 becomes a shuffle, the rest of the list moves to the same place and wants to divide into the same size.For example, if list1 is divided into two, the remaining list is divided into two. Since English is not my first language, my question may be strange. – johnny Choi Nov 18 '18 at 18:41
  • There are two problems here: First, you want to shuffle a group of lists such that all their elements "keep aligned". Decorating, shuffling and undecorating grants this. Then, you want to divide de lists somehow, and a claim to simply apply the same method to all lists, as their elements are aligned as they were originally. This answer shows how to split a list in evenly sized chunks: https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks . – Vitor SRG Nov 18 '18 at 23:16
0

It seems like you want to be doing stuff in arrays, and not in lists of lists.

import numpy as np

list1 = [1,2,3,4,5]
list2 = [2,3,4,1,2]
list3 = [2,4,6,2,1]

arr = np.array([list1, list2, list3])

def random_index(arr, size=None, replace=True):
    return np.random.choice(range(arr.shape[1]), size=size, replace=replace)

for idx in range(10):
    print(arr[:, random_index(arr, size=2, replace=False)])

This, for example - you could replace random_index with anything that gives you a valid integer index of items, depending on what you want to do next.

CJR
  • 3,916
  • 2
  • 10
  • 23