리스트 위치 바꾸기
list = [1,2,3,4,5]에서 2와 3의 위치를 바꾸고 싶다. 이럴 때 list[2] = list[1] 이런 식으로 바꾸면 그 다음 단계에서 막히게 된다. 이를 해결할 수 있는 방법이 list[1], list[2] = list[2], list[1] 이다. list[1]에 list[2]의 값이 할당되고 list[2]에 list[1]의 값이 할당된다. 아래는 이를 이용한 문제 풀이이다.
a, b = map(int, input().split())
l = [i for i in range(1, a+1)]
for _ in range(b):
x, y = map(int, input().split())
l[x-1], l[y-1] = l[y-1], l[x-1]
print(*l)
'PYTHON' 카테고리의 다른 글
[Python] remove, del, pop 차이 (0) | 2023.06.04 |
---|---|
[Python] sort, sorted (feat. 백준 10814번) (2) | 2023.06.03 |
[Python] split(), join() (Feat. 백준 1032번) (0) | 2023.05.30 |
[Python] 함수의 매개변수 (feat. 백준 15596번) (0) | 2023.05.30 |
[Python] not in (feat. 백준 5597번) (0) | 2023.05.29 |