Randomize 2 lists for for loop

Suppose I have 2 lists of the same size along with the following code:

list1 = ['tom', 'mary', 'frank', 'joe']
list2 = [1, 2, 3, 4]

for names, numbers in zip(list1, list2):
    print names, numbers

How to use a random index from each list for each iteration of a for loop?

At the same time, if I have 2 lists of different sizes:

list1 = ['tom', 'mary', 'frank', 'joe', 'john', 'barry']
list2 = [1, 2, 3, 4]

How can I, like in the first example, use random indexes, but this time when it reaches the first index for an item that is outside of list2, start randomizing the index for list2 again for the rest of the elements in list1? Is zip no longer the right way to use in this case?

frank 3
tom 4
john 2
mary 1
barry 4
joe 2

^ desired result (but completely random)

+1
source share
7 answers

, :

import random

def shuffled(seq):
  copy = list(seq)
  random.shuffle(copy)
  return copy

def rand_repeat(seq):
  while True:
    for el in shuffled(seq):
      yield el

list1 = ['tom', 'mary', 'frank', 'joe', 'john', 'barry']
list2 = [1, 2, 3, 4]

print zip(shuffled(list1), rand_repeat(list2))
+6
list1 = ['tom', 'mary', 'frank', 'joe', 'john', 'barry']
list2 = [1, 2, 3, 4]

random.shuffle(list2)
for name, number in zip(list1, itertools.cycle(list2)):
    print name, number
+3

, , 1 2:

while list1:
    for item in random.sample(list2, len(list2)):
        if not list1:
            break
        name = list1.pop(random.randrange(0, len(list1)))
        print name, item

Nota: random.sample(list2, len(list2)) - , .

,

+3

- , , list set, . :

for element in set(list1):
    print element, random.choice(list2)

, , random.shuffle, , , .

, , , . - :

class RandomList(object):
    def __init__(self, master_list):
        self.current_list = master_list
        self.master_list = master_list

    def __iter__(self):
        if not self.current_list:
            self.current_list = self.master_list
        index = random.choice(range(len(self.current_list)))
        value = self.current_list.pop(index)
        return value
+1

, , , , , .


izip_longest random.choice:

list1 = ['tom', 'mary', 'frank', 'joe', 'john', 'barry']
list2 = [1, 2, 3, 4]

izip_longest , zip , , None ( ):

>>> list(izip_longest(list1, list2))
[('tom', 1), ('mary', 2), ('frank', 3), ('joe', 4), ('john', None), ('barry', None)]

None random.choice:

>>> l = [(n, v if v else random.choice(list2)) for n,v in izip_longest(list1, list2)]
>>> l
[('tom', 1), ('mary', 2), ('frank', 3), ('joe', 4), ('john', 2), ('barry', 4)]

, random.shuffle:

>>> random.shuffle(l)
>>> l
[('mary', 2), ('john', 1), ('barry', 2), ('joe', 4), ('tom', 1), ('frank', 3)]
+1

, - itertools.product random.choice:

import random, itertools
>>> def randomizer(a, b):
...     prod = list(itertools.product(a, b))
...     while True:
...         yield random.choice(prod)
...
>>> r = randomizer('tom mary frank joe'.split(' '), range(1, 5))
>>> r.next()
('mary', 2)
>>> r.next()
('frank', 2)
>>> r.next()
('mary', 4)
>>> r.next()
('frank', 3)
>>> r.next()
('joe', 1)

:

name, index = r.next()

, len(a) * len(b), .

0

:

from collections import deque
from random import shuffle
from copy import copy

def make_deque(vals):
        back = copy(vals)
        shuffle(back)
        return deque(back)

prime = ['a','b','c','d','e']
support = [1,2,3,4]
deq = make_deque(support)
shuffle(prime)

for p in prime:
        try:
                s = deq.pop()
        except IndexError:
                deq = make_deque(support)
                s = deq.pop()
        print str(p) + " " + str(s)
0

Source: https://habr.com/ru/post/1525694/


All Articles