문자열 배열의 길이를 요소별로 반환하려면 PythonNumpy에서 numpy.char.str_len() 메서드를 사용합니다. 이 메서드는 정수의 출력 배열을 반환합니다.
단계
먼저 필요한 라이브러리를 가져옵니다 -
import numpy as np
1차원 문자열 배열 생성 -
arr = np.array(['Amy', 'Scarlett', 'Katie', 'Brad', 'Tom'])
배열 표시하기 -
print("Array...\n",arr) 데이터 유형 가져오기 -
print("\nArray datatype...\n",arr.dtype)
배열의 차원 가져오기 -
print("\nArray Dimensions...\n",arr.ndim) 배열의 모양 가져오기 -
print("\nOur Array Shape...\n",arr.shape) 배열의 요소 수 가져오기 -
print("\nNumber of elements in the Array...\n",arr.size) 문자열 배열의 길이를 요소별로 반환하려면 numpy.char.str_len() 메서드를 사용하십시오 -
print("\nResult (length element-wise)...\n",np.char.str_len(arr))
예시
import numpy as np
# Create a One-Dimensional array of strings
arr = np.array(['Amy', 'Scarlett', 'Katie', 'Brad', 'Tom'])
# Displaying our array
print("Array...\n",arr)
# Get the datatype
print("\nArray datatype...\n",arr.dtype)
# Get the dimensions of the Array
print("\nArray Dimensions...\n",arr.ndim)
# Get the shape of the Array
print("\nOur Array Shape...\n",arr.shape)
# Get the number of elements of the Array
print("\nNumber of elements in the Array...\n",arr.size)
# To return the length of a string array element-wise, use the numpy.char.str_len() method in Python Numpy
# The method returns an output array of integers.
print("\nResult (length element-wise)...\n",np.char.str_len(arr)) 출력
Array... ['Amy' 'Scarlett' 'Katie' 'Brad' 'Tom'] Array datatype... <U8 Array Dimensions... 1 Our Array Shape... (5,) Number of elements in the Array... 5 Result (length element-wise)... [3 8 5 4 3]