본문 바로가기
IT/Programming Language

[Python] 행렬 회전

by FreeYourMind 2022. 3. 23.

zip 함수를 이용해 2차원 배열 회전

mylist = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_list = list(map(list, zip(*mylist)))

- mylist 안의 각 배열에서 첫 번째 원소를 뽑아 list로 만드는 과정을 차례로 진행 후 다시 list로 만듬

-> 각 행의 첫 번째 원소가 첫 번째 열로 바뀜

 

* zip 

- 각 인자에서 요소를 하나씩 뽑아 묶어줌

mylist = [1, 2, 3]
new_list = [40, 50, 60]
for i in zip(mylist, new_list): print (i) # (1, 40) (2, 50), (3, 60)

list(zip("abc", "def")) # [('a', 'd'), ('b', 'e'), ('c', 'f')]
animals = ['cat', 'dog', 'lion']
sounds = ['meow', 'woof', 'roar']
answer = dict(zip(animals, sounds)) # {'cat': 'meow', 'dog': 'woof', 'lion': 'roar'}

 

* map(f, iterable)

- 함수(f)와 반복 가능한(iterable) 자료형을 입력 받음

- 입력 받은 자료형의 각 요소를 함수 f가 수행한 결과를 묶어서 돌려주는 함수

def two_times(x): 
	return x*2
list(map(two_times, [1, 2, 3, 4])) # [2, 4, 6, 8]

list(map(lambda a: a*2, [1, 2, 3, 4])) # [2, 4, 6, 8]

 

출처

https://programmers.co.kr/learn/courses/4008/lessons/13318

https://wikidocs.net/32

'IT > Programming Language' 카테고리의 다른 글

[Python] Class  (0) 2022.04.15
[Python] 까먹을 만한 문법 정리  (0) 2022.03.22
[Python] switch -> match  (0) 2022.03.22
Generic이란  (0) 2022.03.06
[Javascript] 비동기 처리 방식  (0) 2022.02.20

댓글