jeudi 1 juillet 2021

randomly select elements in a nested list one time

I apologize if this is an odd question, non computer science person here.

Is there an algorithmic way that I could "randomly" select elements in a nested list, but not make a repeated selection? I am trying to figure out a way to randomly select elements in the nested list only once until all elements have been selected.

This is my nested list:

devices = [['radio/36'],
            ['radio/38',
            'radio/31'],
            ['radio/21',
            'radio/29'],
            ['radio/25',
            'radio/9',
            'radio/6'],
            ['radio/13',
            'radio/14',
            'radio/30'],
            ['radio/19',
            'radio/8',
            'radio/26',
            'radio/24'],
            ['radio/34',
            'radio/11',
            'radio/27',
            'radio/20',
            'radio/23'],
            ['radio/15',
            'radio/37',
            'radio/39',
            'radio/10']]

For example there is nothing in the code below that will prevent selecting or printing something twice.

import random

for i in range(len(devices)):
    random_pick = devices[random.randint(0, len(devices))]
    print(random_pick)

The code above is definitely lacking a lot and has the potential to print something twice or completely miss an element.

['radio/19', 'radio/8', 'radio/26', 'radio/24']
['radio/38', 'radio/31']
['radio/19', 'radio/8', 'radio/26', 'radio/24']
['radio/36']
['radio/36']
['radio/15', 'radio/37', 'radio/39', 'radio/10']
['radio/36']
['radio/36']

Any tips on how to think through this problem greatly appreciated.




Aucun commentaire:

Enregistrer un commentaire