-
-
Save parzibyte/5e462941e5d4bf501a219c1d22cc20a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
https://parzibyte.me/blog | |
""" | |
import random | |
def mezclar_lista(lista_original): | |
# Crear una copia, ya que no deberíamos modificar la original | |
# https://parzibyte.me/blog/2020/05/31/python-clonar-lista-eliminar-referencia/ | |
lista = lista_original[:] | |
# Ciclo for desde 0 hasta la longitud de la lista -1 | |
longitud_lista = len(lista) | |
for i in range(longitud_lista): | |
# Obtener un índice aleatorio | |
# https://parzibyte.me/blog/2019/04/04/generar-numero-aleatorio-rango-python/ | |
indice_aleatorio = random.randint(0, longitud_lista - 1) | |
# Intercambiar | |
temporal = lista[i] | |
lista[i] = lista[indice_aleatorio] | |
lista[indice_aleatorio] = temporal | |
# Regresarla | |
return lista | |
lista = [1, 2, 3, 4, 5, 6, 7, 8, 9] | |
lista_mezclada = mezclar_lista(lista) | |
print("Lista original: ") | |
print(lista) | |
print("Lista aleatoria: ") | |
print(lista_mezclada) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment