NumPy 教程目录

1 字符串信息函数

1.1 numpy.char.count

  char.count(a, sub, start=0, end=None) 返回一个数组,其中包含 [start, end] 范围内子字符串 sub 的非重叠出现次数。

Example:

c = np.array(['aAaAaA', '  aA  ', 'abBABba'])
print(c)
print(np.char.count(c, 'A'))
print(np.char.count(c, 'aA'))
print(np.char.count(c, 'A', start=1, end=4))
print(np.char.count(c, 'A', start=1, end=3))
"""
['aAaAaA' ' aA ' 'abBABba']
[3 1 1]
[3 1 0]
[2 1 1]
[1 0 0]
"""

1.2 numpy.char.endswith

   char.endswith(a, suffix, start=0, end=None) 返回一个布尔数组,该数组为 True,其中 a 中的字符串元素以后缀结尾,否则为 False

Example:

print(np.char.endswith(['Blair','Jane'],'r'))
print(np.char.endswith(['Blair','Jane'],'r',start=1,end=2))
print(np.char.endswith(['Blair','Jane'],'r',start=3,end=5))
s = np.array(['foo', 'bar'])
print(np.char.endswith(s, 'ar'))
print(np.char.endswith(s, 'a', start=1, end=2))
"""
[ True False]
[False False]
[ True False]
[False True]
[False True]
"""

1.3 numpy.char.find

  char.find(*a*, *sub*, *start=0*, end=None) 对于每个元素,返回字符串中找到子字符串 sub 的最低索引。

Example:

s = ['Blair','Jane',"Lee"]
print(np.char.find(s,'J'))
print(np.char.find(s,'a'))
print(np.char.find(s,'e',start=0,end=2))
"""
[-1 0 -1]
[ 2 1 -1]
[-1 -1 1]
"""

相关方法:

  • numpy.char.rfind

  char.rfind(a, sub, start=0, end=None) 对于 a 中的每个元素,返回找到子字符串 sub 的字符串中的最高索引,使得 sub 包含在 [start, end] 中。

Example:

s = ['JBlaJir','JaJne',"Lee"]
print(np.char.rfind(s,'J'))
print(np.char.rfind(s,'a'))
print(np.char.rfind(s,'e',start=0,end=2))
"""
[ 4 2 -1]
[ 3 1 -1]
[-1 -1 1]
"""

1.4 numpy.char.index

  char.index(a, sub, start=0, end=None) 与 find 类似,但在未找到子字符串时引发 ValueError

Example:

s = ['Blair','Jane',"Lee"]
print(np.char.index(s,'J'))
print(np.char.index(s,'a'))
print(np.char.index(s,'e',start=0,end=2))
"""
ValueError
ValueError
ValueError
""" s = ['Blaie','Jane',"Lee"]
print(np.char.index(s,'e',start=0,end=2))
"""
ValueError
""" s = ['Blaie','Jane',"Lee"]
print(np.char.index(s,'e',start=0,end=5))
"""
[4 3 1]
"""

相关方法:

  • numpy.char.rindex

  char.rindex(a, sub, start=0, end=None) 与 rfind 类似,但在未找到子字符串 sub 时引发 ValueError

Example:

s = ['JBlaJire','JaJne',"JaLee"]
print(np.char.rindex(s,'J'))
print(np.char.rindex(s,'a'))
print(np.char.rindex(s,'e',start=0,end=2))
"""
[ 4 2 -1]
[ 3 1 -1]
ValueError
"""

1.5 numpy.char.isalpha

  char.isalpha(a) 如果字符串中的所有字符都是字母并且至少有一个字符,则为每个元素返回 true,否则返回 false

Example:

s = ['Blaie','1111',"Lee"]
print(np.char.isalpha(s))
s = ['Blaie1','反差萌',"Lee"]
print(np.char.isalpha(s))
s = ['Blaie1','#####',"Lee"]
print(np.char.isalpha(s))
"""
[ True False True]
[False True True]
[False False True]
"""

1.6 numpy.char.isalnum

  char.isalnum(a) 如果字符串中的所有字符都是字母数字并且至少有一个字符,则为每个元素返回 true,否则返回 false

Example:

s = ['Blaie','1111',"Lee"]
print(np.char.isalnum(s))
s = ['Blaie1','反差萌',"Lee"]
print(np.char.isalnum(s))
s = ['Blaie1','#####',"Lee"]
print(np.char.isalnum(s))
"""
[ True True True]
[ True True True]
[ True False True]
"""

1.7 numpy.char.isdecimal

  char.isdecimal(a) 对于每个元素,如果元素中只有十进制字符,则返回 True

Example:

s = ['Blaie','1111',"Lee"]
print(np.char.isdecimal(s))
s = ['Blaie1','反差萌',"Lee"]
print(np.char.isdecimal(s))
s = ['Blaie1','#####',"Lee"]
print(np.char.isdecimal(s))
s = ['1.1111','1+2j',"Lee"]
print(np.char.isdecimal(s))
"""
[False True False]
[False False False]
[False False False]
[ True False False]
"""

1.8 numpy.char.isdigit

  char.isdigit(a) 如果字符串中的所有字符都是数字并且至少有一个字符,则为每个元素返回 true,否则返回 false

Example:

s = ['Blaie','1111',"Lee007",'1.11','1+1j','反差萌','###']
print(np.char.isdigit(s))
"""
[False True False False False False False]
"""

1.9 numpy.char.islower

  char.islower(a) 如果字符串中的所有大小写字符都是小写并且至少有一个大小写字符,则为每个元素返回 true,否则返回 false

Example:

s = ['Blaie','1111',"Lee007",'1.11','1+1j','反差萌','###','abc','ABC','1+2j#']
print(np.char.islower(s))
"""
[False False False False True False False True False True]
"""

1.10 numpy.char.isnumeric

  char.isnumeric(a) 对于每个元素,如果元素中只有数字字符,则返回 True

Example:

s = ['Blaie','1111',"Lee007",'1.11','1+1j','反差萌','###','abc','ABC','1+2j#']
print(np.char.isnumeric(s)) #只有全是数字时返回True
"""
[False True False False False False False False False False]
"""

1.11 numpy.char.isspace

  char.isspace(a) 如果字符串中只有空白字符并且至少有一个字符,则为每个元素返回 true,否则返回 false

Example:

s = ['Blaie','1111',"Lee007",'1.11','1+1j','反差萌','###','abc','ABC','1+2j#','',' ','\t']
print(np.char.isspace(s)) #只有全是空白字符且至少有一个时返回True
"""
[False False False False False False False False False False False True True]
"""

1.12 numpy.char.istitle

  char.istitle(a) 如果元素是一个标题字符串并且至少有一个字符,则为每个元素返回 true,否则返回 false

Example:

s = ['T','Blaie','1111',"Lee007",'1.11','1+1j','反差萌','###','abc','ABC','1+2j#','',' ','\t']
print(np.char.istitle(s)) #只有全是空白字符且至少有一个时返回True
"""
[ True True False True False False False False False False False False
False False]
"""

1.13 numpy.char.isupper

  char.isupper(a) 如果字符串中的所有大小写字符都是大写并且至少有一个字符,则为每个元素返回 true,否则返回 false

Example:

s = ['T','Blaie','1111',"Lee007",'1.11','1+1j','反差萌','###','abc','ABC','1+2j#','',' ']
print(np.char.isupper(s)) #只有全是空白字符且至少有一个时返回True
i = 0
for tmp in np.char.isupper(s):
print('%-6s -> %-5s'%(s[i],tmp))
i = i+1
"""
[ True False False False False False False False False True False False
False]
T -> True
Blaie -> False
1111 -> False
Lee007 -> False
1.11 -> False
1+1j -> False
反差萌 -> False
### -> False
abc -> False
ABC -> True
1+2j# -> False
-> False
-> False
"""

1.14 numpy.char.startswith

  char.startswith(a, prefix, start=0, end=None) 返回一个布尔数组,该数组为 True,其中 a 中的字符串元素以前缀开头,否则为 False

Example:

print(np.char.startswith(['Blair','Jane'],'r'))
print(np.char.startswith(['Blair','Jane'],'r',start=1,end=2))
print(np.char.startswith(['Blair','Jane'],'r',start=3,end=5))
s = np.array(['foo', 'bar'])
print(np.char.startswith(s, 'ar'))
print(np.char.startswith(s, 'a', start=1, end=2))
"""
[False False]
[False False]
[False False]
[False False]
[False True]
"""

1.15 numpy.char.str_len

  char.str_len(a) 返回 len(a) 元素。

Example:

s = ['T','Blaie','1111',"Lee007",'1.11','1+1j','反差萌','###','abc','ABC','1+2j#','',' ']
print(np.char.str_len(s))
"""
[1 5 4 6 4 4 3 3 3 3 5 0 1]
"""

Lesson14——NumPy 字符串函数之 Par3:字符串信息函数的更多相关文章

  1. MySQL字符串函数substring:字符串截取

    MySQL 字符串截取函数:left(), right(), substring(), substring_index().还有 mid(), substr().其中,mid(), substr() ...

  2. C++字符串函数与C字符串函数比较

    赋值拷贝: #include <iostream> #include <string> using namespace std; void main(){ string a=& ...

  3. Oracle截取字符串函数和查找字符串函数,连接运算符||

    参考资料:Oracle截取字符串和查找字符串 oracle自定义函数学习和连接运算符(||) oracle 截取字符(substr),检索字符位置(instr) case when then else ...

  4. 字符串函数(strcpy字符串拷,strcmp字符串比较,strstr字符串查找,strDelChar字符串删除字符,strrev字符串反序,memmove拷贝内存块,strlen字符串长度)

    1.strcpy字符串拷贝拷贝pStrSource到pStrDest,并返回pStrDest地址(源和目标位置重叠情况除外) char *strcpy(char *pStrDest, const ch ...

  5. javascript函数一共可分为五类: ·常规函数 ·数组函数 ·日期函数 ·数学函数 ·字符串函数

    javascript函数一共可分为五类:    ·常规函数    ·数组函数    ·日期函数    ·数学函数    ·字符串函数    1.常规函数    javascript常规函数包括以下9个 ...

  6. PHP 常用字符串函数整理

    PHP语言中的字符串函数也是一个比较易懂的知识.今天我们就为大家总结了将近12种PHP字符串函数,希望对又需要的朋友有所帮助,增加读者朋友的PHP知识库. 1.查找字符位置函数 strpos($str ...

  7. PHP部分字符串函数汇总

    PHP部分字符串函数汇总 提交 我的评论 加载中 已评论 PHP部分字符串函数汇总 2015-03-10 PHP100中文网 PHP100中文网 PHP100中文网 微信号 功能介绍 互联网开发者社区 ...

  8. 5、SQL基础整理(字符串函数)

    字符串函数 ASCII 返回字符串首字母的ascii编码 select ASCII('name') select ASCII(name) from xuesheng select *from xues ...

  9. (基础篇)PHP字符串函数

    1查找字符位置函数:   strpos($str,search,[int]):查找search在$str中的第一次位置从int开始: stripos($str,search,[int]):函数返回字符 ...

随机推荐

  1. [服务器部署] Flask + virtualenv + uWSGI + Nginx 遇到的问题

    1.配置好了Flask + virtualenv +uWSGI,启动uWSGI并调试,网页显示 Internal Server Error 参考:https://www.cnblogs.com/cle ...

  2. Python2 和 Python3 共存于 Centos7

    一.解决Python2 pip问题 centos7自带的是Python2,但是并没有安装pip,我们需要自行安装 包名为 python-pip # yum install epel-release - ...

  3. SCryptPasswordEncoder 单向加密 --- 心得

    1.前言 * BCryptPasswordEncoder相关知识:* 用户表的密码通常使用MD5等不可逆算法加密后存储,为防止彩虹表破解更会先使用一个特定的字符串(如域名)加密,然后再使用一个随机的s ...

  4. nuxt中iview按需加载配置

    在plugins/iview.js中修改 初始代码如下 import Vue from 'vue' import iView from 'iview' import locale from 'ivie ...

  5. HDU 1576 A/B (两种解法)

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 分析:等式枚举法,由题意可得:, ,代入 ,    得:,把变量 合在一起得: :即满足 为 倍 ...

  6. T-SQL创建数据库常用方法2020年10月29日20:12:04网课笔记

    2.接口的作用 第一.方便框架的设计.利于团队的开发. 第二.方便项目拓展.高内聚.低耦合. 3.反射 [1]反射的理解:通过读取程序集的信息,找到相关的类型和类型的成员,也可以得到相关的对象.而这种 ...

  7. 生产环境上,哨兵模式集群Redis版本升级应用实战

    背景: 由于生产环境上所使用的Redis版本并不一致,好久也没有更新,为了避免版本不同对Redis集群造成影响,从而升级为统一Redis版本! 1.集群架构 一主两从三哨兵: 2.升级方案 (1)升级 ...

  8. 求n以内最大的k个素数以及它们的和

    本题要求计算并输出不超过n的最大的k个素数以及它们的和. 输入格式: 输入在一行中给出n(10≤n≤10000)和k(1≤k≤10)的值. 输出格式: 在一行中按下列格式输出: 素数1+素数2+-+素 ...

  9. 【解决了一个小问题】golang build中因为缓存文件损坏导致的编译错误

    编译的过程中出现了一个吓人的错误: GOROOT=C:\Go #gosetup GOPATH=C:\Users\ahfuzhang\go #gosetup C:\Go\bin\go.exe mod t ...

  10. JavaCV推流实战(MP4文件)

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...