2

I would like to get 20 random results from the following list:

coordinates = [
  [20, 140], [40, 140], [60, 140], [80, 140], [100, 140], [120, 140],
  [20, 120], [40, 120], [60, 120], [80, 120], [100, 120], [120, 120],
  [20, 100], [40, 100], [60, 100], [80, 100], [100, 100], [120, 100],
  [20, 80], [40, 80], [60, 80], [80, 80], [100, 80], [120, 80],
  [20, 60], [40, 60], [60, 60], [80, 60], [100, 60], [120, 60],
  [20, 40], [40, 40], [60, 40], [80, 40], [100, 40], [120, 40]
]

I tried random.shuffle but it returns None.

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
Thom
  • 197
  • 1
  • 2
  • 7

5 Answers5

12

If you want 20 unique values in random order, use random.sample():

random.sample(coordinates, 20)
random.sample(population, k)¶

Return a k length list of unique elements chosen from the population sequence or set. Used for random sampling without replacement.

>>> random.sample(coordinates, 20)
[[80, 60], [40, 100], [80, 100], [60, 80], [60, 100], [40, 60], [40, 80], [80, 120], [120, 140], [120, 100], [100, 80], [40, 120], [80, 140], [100, 140], [20, 80], [120, 80], [100, 100], [20, 40], [120, 120], [100, 120]]

You could use random.choice() 20 times, but this will not be "unique"—elements may be duplicated, because one is randomly selected each time:

>>> [random.choice(coordinates) for _ in range(20)]
[[80, 80], [40, 140], [80, 140], [60, 60], [120, 100], [20, 120], [100, 80], [120, 100], [20, 60], [100, 120], [100, 40], [80, 80], [100, 80], [80, 120], [20, 40], [100, 80], [60, 80], [80, 140], [40, 40], [120, 40]]
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
Will
  • 24,082
  • 14
  • 97
  • 108
  • 1
    If you need random choice with replacement, use [`random.choices`](https://docs.python.org/library/random.html#random.choices) (new in Python 3.6) instead of using `random.choice` in a list comprehension. – Boris Verkhovskiy Dec 21 '19 at 02:59
6

I think you might be looking for random.sample from the random library. You can use like this:

import random
my_new_list = random.sample(coordinates, 20)
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
Leukonoe
  • 649
  • 2
  • 10
  • 29
2

One way of doing it is to use random.shuffle. Instead of returning a new shuffled list, shuffle modifies the list in place, which is why it wasn't returning anything.

from random import shuffle
shuffle(coordinates)
result = coordinates[:20]
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
Matthias Schreiber
  • 2,347
  • 1
  • 13
  • 20
2

random.shuffle shuffles the input parameter directly and returns nothing.

With random.shuffle:

import random
random.shuffle(coordinates)[:20]

But it changes your source list. My opinion: it's not good.

You can use random.sample

import random
random.sample(coordinates, 20)
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
qvpham
  • 1,896
  • 9
  • 17
-2

Try the following recipe:

coordinaten = [[20, 140], [40, 140], [60, 140], [80, 140], [100, 140], [120, 140],
           [20, 120], [40, 120], [60, 120], [80, 120], [100, 120], [120, 120],
           [20, 100], [40, 100], [60, 100], [80, 100], [100, 100], [120, 100],
           [20, 80], [40, 80], [60, 80], [80, 80], [100, 80], [120, 80],
           [20, 60], [40, 60], [60, 60], [80, 60], [100, 60], [120, 60],
           [20, 40], [40, 40], [60, 40], [80, 40], [100, 40], [120, 40]]

import random

print(random.sample(coordinaten, 20))
armatita
  • 12,825
  • 8
  • 48
  • 49
Ishaq Khan
  • 173
  • 2
  • 9