본문 바로가기
Computer Science/[20-4] Numpy 연습

[Numpy] (3) Concatenate로 여러개의 배열 합치기

by gojw 2020. 3. 31.

HackerRank 문제: https://www.hackerrank.com/challenges/np-concatenate/problem

 

Concatenate | HackerRank

Use the concatenate function on 2 arrays.

www.hackerrank.com

import numpy as np

first = input().split(' ')
rows1, rows2= int(first[0]), int(first[1])

data1 = []
for _ in range(rows1):
    next = input().split(' ')
    data1.append([int(x) for x in next])
data1 = np.array(data1)

data2 = []
for _ in range(rows2):
    next = input().split(' ')
    data2.append([int(x) for x in next])
data2 = np.array(data2)

print(np.concatenate((data1, data2)))

 

import numpy as np

first = input().split(' ')
rows, columns = int(first[0]) + int(first[1]), int(first[2])

data = []
for _ in range(rows):
    next = input().split(' ')
    data.append([int(x) for x in next])
    
result = np.array(data)
print(result)

→ .concatenate() 안쓰고

 

numpy.concatenate(array name, axis=0, out=None)

axis parameter에 넣은 값대로 배열을 합쳐준다.

axis에 None을 넣으면 flatten된다. 디폴트 값은 0.

 

예시)

>>> array1 = np.array([[1, 2], [3, 4]])
>>> array2 = np.array([[5, 6], [7, 8]])

>>> print(np.concatenate((array1, array2), axis=None))
# [1 2 3 4 5 6 7 8]

댓글