numpy.count () в Python

Опубликовано: 26 Марта, 2022

numpy.core.defchararray.count(arr, substring, start=0, end=None): Counts for the non-overlapping occurrence of sub-string in the specified range.

Parameters:
arr : array-like or string to be searched.
substring : substring to search for.
start, end : [int, optional] Range to search in.

Returns : An integer array with the number of non-overlapping occurrences of sub-string.

Code #1:

# Python Program illustrating 
# numpy.char.count() method 
  
import numpy as np 
  
# 2D array 
arr = ["vdsdsttetteteAAAa", "AAAAAAAaattttds", "AAaaxxxxtt", "AAaaXDSDdscz"]
  
print ("arr : ", arr)
  
print ("Count of "tt"", np.char.count(arr, "tt"))
print ("Count of "tt"", np.char.count(arr, "tt", start = 0))
print ("Count of "tt"", np.char.count(arr, "tt", start = 8))

Выход:

arr :  ["vdsdsttetteteAAAa", "AAAAAAAaattttds", "AAaaxxxxtt", "AAaaXDSDdscz"]
Count of "tt" [2 2 1 0]
Count of "tt" [2 2 1 0]
Count of "tt" [1 2 1 0]

 
Code #2:

# Python Program illustrating 
# numpy.char.count() method 
import numpy as np 
  
# 2D array 
arr = ["vdsdsttetteteAAAa", "AAAAAAAaattttds", "AAaaxxxxtt", "AAaaXDSDdscz"]
  
print ("arr : ", arr)
  
print ("Count of "Aa"", np.char.count(arr, "Aa"))
print ("Count of "Aa"", np.char.count(arr, "Aa", start = 8))

Выход:

arr: ['vdsdsttetteteAAAa', 'AAAAAAAaattttds', 'AAaaxxxxtt', 'AAaaXDSDdscz']

Счетчик "Аа" [1 1 1 1]
Счетчик "Аа" [1 0 0 0]

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.