Python para impacientes

Python como usar o sample

January 04, 2020

Returns a random element from a list.

Use random.randint() to generate a random number that corresponds to an index in the list, return the element at that index.

random.sample() provides similar functionality to this snippet.

from random import randint

def sample(lst):
  return lst[randint(0, len(lst) - 1)]
sample([3, 7, 9, 11]) # 9

Acesse a Referência original 1: Acesse a Referência original 2: