Questions about Python random_chunk.

Asked 1 years ago, Updated 1 years ago, 81 views

We are making a function that receives 2 lists and cuts randomly after shuffling. I even followed the location when it became random, but I didn't know how to cut it together during the cutting process, so I asked.

def random_chunk(li,b, min_chunk=1, max_chunk=3):
    it = iter(li)
    bb = iter(b)
    while True: #2 I hope the list is cut to the same size.
        nxt = list(islice(it,randint(min_chunk,max_chunk)))
        if nxt:
            yield nxt
        else:
            break
#example
a = [1,2,3,4]
b = [5,6,7,8]

slice = list(random_chunk(a,b))

#print
a = [[2,1], [4,3]]
b = [[6,5], [8,7]]
or 
a = [[3,2,1],[4]]
b = [[7,6,5],[8]]

python python-2.7

2022-09-21 17:19

1 Answers

Refer to the code below.

I'd like to work on each list.

import random

a = [1,2,3,4]
b = [5,6,7,8]

random.shuffle(a)
random.shuffle(b)

cnt = 3
print [a[n:n + cnt] for n in range(0, len(a), cnt)]
print [b[n:n + cnt] for n in range(0, len(b), cnt)]

cnt = 2
print [a[n:n + cnt] for n in range(0, len(a), cnt)]
print [b[n:n + cnt] for n in range(0, len(b), cnt)]


2022-09-21 17:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.