“Cambiar dos elementos en una lista Python” Código de respuesta

función de intercambio de pitón

def swap0(s1, s2):
    assert type(s1) == list and type(s2) == list
    tmp = s1[:]
    s1[:] = s2
    s2[:] = tmp
    
# However, the easier and better way to do a swap in Python is simply:
s1, s2 = s2, s1
Gorgeous Goshawk

Cambiar dos elementos en una lista Python

# Python3 program to swap elements
# at given positions
 
# Swap function
def swapPositions(list, pos1, pos2):
     
    list[pos1], list[pos2] = list[pos2], list[pos1]
    return list
 
# Driver function
List = [23, 65, 19, 90]
pos1, pos2  = 1, 3
 
print(swapPositions(List, pos1-1, pos2-1))
Lilian Bosc

Respuestas similares a “Cambiar dos elementos en una lista Python”

Preguntas similares a “Cambiar dos elementos en una lista Python”

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código