Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting a random sublist from a list in Python

I have a python dictionary as follows:

{'APPLE_PROVIDERS' : ["some", "provider","can","be", "null"],
  ....
}

What i want to do is get a random sublist from the list (which is a value) of a key. Not just one element, but a totally random sublist. Here is what I tried:

a_list = a_dict.get('APPLE_PROVIDERS', "")
for item in a_list[randrange(0,len(a_list)) : randrange(0,len(a_list))]:
  ...do something.. 

This thing has two problems :

  1. If the list is empty, or if the dict lookup fails, the program fails since randrange has (0,0) as arguments, which results in an error

  2. Many times both randrange() calls generate the same number, especially when the list is small. This returns an empty list. For example a_list[5:5]

So what is the best way to get a random sublist with above cases handled ? Also, I do not care about the ordering. Anything works. I just want a totally random sublist of either 0,1... till len(a_list) elements each time the for loop starts.

If the list can be changed in some other data structure which can hold similar elements, that works for me too.

like image 655
user775093 Avatar asked Nov 25 '15 06:11

user775093


People also ask

How do I randomly select multiple items from a list in Python?

Use the random. sample() function when you want to choose multiple random items from a list without repetition or duplicates. There is a difference between choice() and choices() . The choices() was added in Python 3.6 to choose n elements from the list randomly, but this function can repeat items.

How do you make a sublist from a list in Python?

To get the subarray we can use slicing to get the subarray. Step 1: Run a loop till length+1 of the given list. Step 2: Run another loop from 0 to i. Step 3: Slice the subarray from j to i.

How do I pick a random item from a list in Python?

The simplest way to use Python to select a single random element from a list in Python is to use the random. choice() function. The function takes a single parameter – a sequence. In this case, our sequence will be a list, though we could also use a tuple.

How do you select a random string in Python?

In order to generate random strings in Python, we use the string and random modules. The string module contains Ascii string constants in various text cases, digits, etc. The random module on the other hand is used to generate pseudo-random values.


2 Answers

Sample it.

>>> random.sample(["some", "provider", "can", "be", "null"], 3)
['some', 'can', 'provider']
>>> random.sample(["some", "provider", "can", "be", "null"], 3)
['can', 'null', 'provider']
>>> random.sample(["some", "provider", "can", "be", "null"], 3)
['null', 'some', 'provider']
like image 114
Ignacio Vazquez-Abrams Avatar answered Sep 21 '22 21:09

Ignacio Vazquez-Abrams


>>> from random import randint
>>> left = randint(0, len(L))
>>> right = randint(left, len(L))
>>> L[left:right]
['null']

if you don't want the possiblity of empty lists

>>> left = randint(0, len(L) - 1)
>>> right = randint(left + 1, len(L))
like image 32
John La Rooy Avatar answered Sep 20 '22 21:09

John La Rooy