“Elija un índice aleatorio de la lista de Python” Código de respuesta

Cómo imprimir una parte aleatoria de una lista en Python

import random

foo = ['a', 'b', 'c', 'd', 'e']
print(random.choice(foo))
Careful Caracal

Elija un índice aleatorio de la lista de Python

import random  # Don't forget to install it first with pip install random

ahahfunny = ['ahah ', 'copy-', 'paste ', 'stack overflow',' go ', 'brrr']
#  the biggest index in this list above is 5 (0 being 1 in programming)

print(ahahfunny[random.randint(0, 5)]  # I like this way,
      
print(random.choice(ahahfunny)) # But this way is WAY thiccer...
stackoverflow.com

Seleccione Valor aleatorio de List Python

import random

# with replacement = same item CAN be chosen more than once.
# without replacement = same item CANNOT be chosen more then once.

# Randomly select 2 elements from list without replacement and return a list
random.sample(list_name, 2)

# Randomly select 3 elements from list with replacement and return a list
random.choices(set_name, k=3)

# Returns 1 random element from list
random.choice(list_name)
CodeHelper

Respuestas similares a “Elija un índice aleatorio de la lista de Python”

Preguntas similares a “Elija un índice aleatorio de la lista de Python”

Más respuestas relacionadas con “Elija un índice aleatorio de la lista de Python” en Python

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código