ในการส่งคืนอาร์เรย์ที่มีจำนวนของสตริงย่อยที่ไม่ทับซ้อนกัน ให้ใช้วิธี thenumpy.char.count() ใน Python Numpy พารามิเตอร์แรกคือส่วนย่อย เช่น สตริงย่อยที่จะค้นหา โมดูล numpy.char จัดเตรียมชุดของการดำเนินการสตริงแบบเวกเตอร์สำหรับอาร์เรย์ของ typenumpy.str_
ขั้นตอน
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import numpy as np
สร้างอาร์เรย์หนึ่งมิติของสตริง -
arr = np.array(['kATIE', 'JOHN', 'KAte', 'AmY', 'BRADley'])
กำลังแสดงอาร์เรย์ของเรา -
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) ในการส่งคืนอาร์เรย์ที่มีจำนวนของสตริงย่อยที่ไม่ทับซ้อนกัน ให้ใช้วิธี thenumpy.char.count() ใน Python Nump พารามิเตอร์แรกเป็นพารามิเตอร์ย่อย เช่น สตริงย่อยเพื่อค้นหา -
print("\nResult (count)...\n",np.char.count(arr, 'A'))
ตัวอย่าง
import numpy as np
# Create a One-Dimensional array of strings
arr = np.array(['kATIE', 'JOHN', 'KAte', 'AmY', 'BRADley'])
# 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 an array with the number of non-overlapping occurrences of substring, use the numpy.char.count() method in Python Numpy
# The first parameter is the sub i.e. the substring to search for
print("\nResult (count)...\n",np.char.count(arr, 'A')) ผลลัพธ์
Array... ['kATIE' 'JOHN' 'KAte' 'AmY' 'BRADley'] Array datatype... <U7 Array Dimensions... 1 Our Array Shape... (5,) Number of elements in the Array... 5 Result (count)... [1 0 1 1 1]