当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python numpy.count()用法及代码示例


numpy.core.defchararray.count(arr,substring,start = 0,end = None):在指定范围内不重复出现的子字符串的计数。

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

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



代码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]


代码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']

Count of 'Aa' [1 1 1 1]
Count of 'Aa' [1 0 0 0]


相关用法


注:本文由纯净天空筛选整理自Mohit Gupta_OMG 大神的英文原创作品 numpy.count() in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。