Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Randomly assign a pair to each item in a list without repetitions

Tags:

python

random

So I have a list and I would like to "assign" the values to a different random value.

For eg.

list = ["dog", "cat", "rat", "bird", "monkey"]

I would like an output like

{"dog": "bird", "cat": "monkey", "rat": "dog", "bird": "rat", "monkey": "cat"}

What I would like is:

  • A value can't be assigned to itself e.g not {"cat": "cat"}
  • A value can only be assigned once e.g not {"cat": "dog", "rat": "dog"}
  • Values can't be assigned to each other e.g not {"cat": "dog", "dog", "cat"}

I tried this code:

def shuffle_recur(_list):
    final = {}
    not_done = copy.deepcopy(_list)
    for value in _list:
        without_list = not_done.copy()
        if value in without_list :
            without_list.remove(value)
        if value in final.values():
            for final_key, final_value in final.items():
                if final_value == value:
                    print(final_value, '    ', final_key)
                    if final_key in without_list :
                        without_list.remove(final_key)
        if len(without_list) < 1:
            print('less')
            return shuffle_recur(_list)
        target = random.choice(without_list)
        not_done.remove(target)
        final[value] = target
        print('{} >> {}'.format(value, target))
    return final

But it is very messy and I don't think it the the best way. What would be a better way to do this?

like image 464
MadMan47 Avatar asked May 09 '20 14:05

MadMan47


People also ask

How do you randomly select a list in Python without repeats?

Python's random module provides a sample() function for random sampling, randomly picking more than one element from the list without repeating elements. It returns a list of unique items chosen randomly from the list, sequence, or set. We call it random sampling without replacement.

How do you assign randomly in Python?

To use shuffle, import the Python random package by adding the line import random near the top of your program. Then, if you have a list called x, you can call random. shuffle(x) to have the random shuffle function reorder the list in a randomized way. Note that the shuffle function replaces the existing list.

Which of the following methods of random module is used to pick a single element randomly from a given list of elements choose pick sample choice?

Explanation: In order to pick a single element, randomly, from a given list of elements, choice method is used.


1 Answers

You may just build a random-ordered list of your items, then pair them as key-value

From one hand you'll take the list, in the other hand the same list rotated from on item values[1:] + [values[0]], and you zip both to pair 2-by-2 pairs, and build a dict from these pairs

values = ["dog", "cat", "rat", "bird", "monkey"]
shuffle(values)
result = dict(zip(values, values[1:] + [values[0]]))

Example

  • shuffling gives ['bird', 'dog', 'rat', 'monkey', 'cat']

  • rotating gives ['dog', 'rat', 'monkey', 'cat', 'bird']

  • zipping gives [('bird', 'dog'), ('dog', 'rat'), ('rat', 'monkey'), ('monkey', 'cat'), ('cat', 'bird')]

  • then each pair becomes a mapping

print(values)  # ['bird', 'dog', 'rat', 'monkey', 'cat']
print(result)  # {'bird': 'dog', 'dog': 'rat', 'rat': 'monkey', 'monkey': 'cat', 'cat': 'bird'}

If you don't the mapping to be following each other, just shuffle a second time

mappings = list(zip(values, values[1:] + [values[0]]))
shuffle(mappings)
result = dict(mappings)
like image 67
azro Avatar answered Sep 23 '22 19:09

azro