참고한 곳:
https://programmer-chocho.tistory.com/59
https://tasddc.tistory.com/108
https://kimjingo.tistory.com/205 1
2차원 리스트 접근해보기
# 2차원 리스트의 선언
home = [[0 for _ in range(3)] for _ in range(3)]
print(home)
>> [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
# 리스트 home = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]가 있을 때,
# 1) 가로 요소 (row 고정, col 변화)
temp01 = [[-1 for _ in range(3)] for _ in range(3)]
for i in range(3):
for j in range(3):
temp01[i][j] = home[i][j]
print(temp01)
# [0][0], [0][1], [0][2] || [1][0], [1][1], [1][2] || [2][0], [2][1], [2][2]
>> [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
# 2) 세로 요소 (row 변화, col 고정)
temp02 = [[-1 for _ in range(3)] for _ in range(3)]
for i in range(3):
for j in range(3):
temp02[j][i] = home[i][j]
print(temp02)
# [0][0], [0][1], [0][2] || [1][0], [1][1], [1][2] || [2][0], [2][1], [2][2]
>> [[0, 3, 6], [1, 4, 7], [2, 5, 8]]
# 3) 대각선 접근 ↘️ ↙️ ↗️ ↖️ 네 개
N = len(home)
temp03 = [-1 for _ in range(N)]
for i in range(N):
temp03[i] = home[i][i]
print(temp03)
# [0][0], [1][1], [2][2]
>> [[0, -1, -1], [-1, 4, -1], [-1, -1, 8]]
temp04 = [-1 for _ in range(N)]
for i in range(N):
temp04[i] = home[i][N-i-1]
print(temp04)
# [0][2], [1][1], [2][0]
>> [2, 4, 6]
temp05 = [-1 for _ in range(N)]
for i in range(N):
temp05[i] = home[N-i-1][i]
print(temp05)
# [2][0], [1][1], [0][2]
>> [6, 4, 2]
temp06 = [-1 for _ in range(N)]
for i in range(N):
temp06[i] = home[N-i-1][N-i-1]
print(temp06)
# [2][2], [1][1], [0][0]
>> [8, 4, 0]
3차원 리스트 접근해보기
lane_xy = [[[-1 for _ in range(N)] for _ in range(N)] for _ in range(N)]
plane_yz = [[[-1 for _ in range(N)] for _ in range(N)] for _ in range(N)]
plane_xz = [[[-1 for _ in range(N)] for _ in range(N)] for _ in range(N)]
home = [[[1, 0, 0], [1, 1, 0], [1, 1, 1]], [[1, 0, 0], [1, 1, 0], [1, 1, 1]], [[1, 0, 0], [1, 1, 0], [1, 1, 1]]]
# 각 x-y, y-z, x-z 평면의 가로 요소만 접근
for case in range(3):
for i in range(N):
for j in range(N):
for k in range(N):
if case == 0:
plane_xy[j][k][i] = home[i][j][k]
elif case == 1:
plane_yz[k][i][j] = home[i][j][k]
elif case == 2:
plane_xz[i][k][j] = home[i][j][k]
print(plane_xy)
>> [[[1, 1, 1], [0, 0, 0], [0, 0, 0]], [[1, 1, 1], [1, 1, 1], [0, 0, 0]], [[1, 1, 1], [1, 1, 1], [1, 1, 1]]]
print(plane_yz)
>> [[[1, 1, 1], [1, 1, 1], [1, 1, 1]], [[0, 1, 1], [0, 1, 1], [0, 1, 1]], [[0, 0, 1], [0, 0, 1], [0, 0, 1]]]
print(plane_xz)
>> [[[1, 1, 1], [0, 1, 1], [0, 0, 1]], [[1, 1, 1], [0, 1, 1], [0, 0, 1]], [[1, 1, 1], [0, 1, 1], [0, 0, 1]]]
리스트 회전 시키기
home = [[1,0,0],[1,1,0],[1,1,1]]
rot90 = [[-1 for _ in range(N)] for _ in range(N)]
rot180 = [[-1 for _ in range(N)] for _ in range(N)]
rot270 = [[-1 for _ in range(N)] for _ in range(N)]
for case in range(3):
for i in range(N):
for j in range(N):
if case == 0:
rot90[N-i-1][j] = home[i][j]
if case == 1:
rot180[N-i-1][N-j-1] = home[i][j]
if case == 2:
rot270[i][N-j-1] = home[i][j]
# [2][0], [1][0], [0][0]
# [2][1], [1][1], [0][1]
# [2][2], [1][2], [0][2]
print(rot90)
>> [[1, 1, 1], [1, 1, 0], [1, 0, 0]]
# [2][2], [2][1], [2][0]
# [1][2], [1][1], [1][0]
# [0][2], [0][1], [0][0]
print(rot180)
>> [[1, 1, 1], [0, 1, 1], [0, 0, 1]]
# [0][2], [1][2], [2][2]
# [0][1], [1][1], [2][1]
# [0][0], [1][0], [2][0]
print(rot270)
>> [[0, 0, 1], [0, 1, 1], [1, 1, 1]]
- 삼성 SW 서티 유형별 분석 글 [본문으로]
'도전, 코딩테스트 > Python version' 카테고리의 다른 글
최빈값 구하기 (0) | 2022.10.03 |
---|---|
Dictionary (딕셔너리) (1) | 2022.09.21 |
00. 리스트, 반복문, 조건문 (0) | 2022.09.18 |
01. 수학 문법 (2) | 2022.08.23 |