numpy错题整理03——字符串数组处理
文章目录
自我介绍
-
我是深圳大学大三的一名大学生,未来想要从事数据分析的工作
-
从今天开始学习python相关库
-
第一步是学习numpy!!
-
若有错误,欢迎指出
资料来源
- 资料来源:https://github.com/fengdu78/Data-Science-Notes
numpy版本
- ‘1.20.3’
刷题
String operations
Q1 连接两个字符数组
-
Concatenate x1 and x2.
-
x1 = np.array(['Hello', 'Say'], dtype=str) x2 = np.array([' world', ' something'], dtype=str) np.char.add(x1,x2)
利用add函数
Q2 重复元素
-
Repeat x three time element-wise.
-
np.char.multiply(x,3)
Q3 统一字符数组元素的格式
-
Q3-1. Capitalize the first letter of x element-wise.
-
Q3-2. Lowercase x element-wise.
-
Q3-3. Uppercase x element-wise.
-
Q3-4. Swapcase x element-wise.
-
Q3-5. Title-case x element-wise.
-
capitalize = np.char.capitalize(x) #首字母大写 lower = np.char.lower(X) #所有字母小写 upper = np.char.upper(x) #所有字母大写 swapcase = np.char.swapcase(x) #大小写交换 title = np.char.title(x) #标题写法,每个单词大写 print("capitalize =", capitalize) print("lower =", lower) print("upper =", upper) print("swapcase =", swapcase) print("titlecase =", titlecase)
Q4 居中,左对齐,右对齐
-
Make the length of each element 20 and the string centered / left-justified / right-justified with paddings of
_
-
x = np.array(['hello world', 'say something'], dtype=str) center = np.char.center(x,30) ljust = np.char.ljust(x,30) rjust = np.char.rjust(x,30) print("centered =", centered) print("left =", left) print("right =", right)
Q5 编码,解码
-
Encode x in cp500 and decode it again.
-
x = np.array(['hello world', 'say something'], dtype=str) encode = np.char.encode(x) decode = np.char.decode(x) print("encoded =", encoded) print("decoded =", decoded)
Q6 插入空格
-
Insert a space between characters of x.
-
x = np.array(['hello world', 'say something'], dtype=str) np.char.join(x,' ')
Q7 去除元素头和尾中的某些字母或者空格
-
Q7-1. Remove the leading and trailing whitespaces of x element-wise.
-
Q7-2. Remove the leading whitespaces of x element-wise.
-
Q7-3. Remove the trailing whitespaces of x element-wise.
-
x = np.array([' hello world ', '\tsay something\n'], dtype=str) strip = np.char.strip(x) lstrip = np.char.lstrip(x) rstrip = np.char.rstrip(x) print(x) print("stripped =", strip) print("lstripped =", lstrip) print("rstripped =", rstrip)
Q8 按空格分割字符数组
-
Split the element of x with spaces.
-
x = np.array(['Hello my name is John'], dtype=str) np.char.split(X)
Q9 按换行符分割字符数组
-
Split the element of x to multiple lines.
-
x = np.array(['Hello my name is John'], dtype=str) np.char.splitlines(x)
Q10 为数字字符串补0
-
Make x a numeric string of 4 digits with zeros on its left.
-
x = np.array(['34'], dtype=str) np.char.zfill(x,4)
Q11 替换特定词
-
Replace “John” with “Jim” in x.
-
x = np.array(['Hello my name is John'], dtype=str) np.char.replace(x,'John','Jim')
Comparison
Q12 相等
-
Return x1 == x2, element-wise.
-
x1 = np.array(['Hello', 'my', 'name', 'is', 'John'], dtype=str) x2 = np.array(['Hello', 'my', 'name', 'is', 'Jim'], dtype=str) np.char.equal(x1,x2)
Q13 不相等
-
Return x1 != x2, element-wise.
-
x1 = np.array(['Hello', 'my', 'name', 'is', 'John'], dtype=str) x2 = np.array(['Hello', 'my', 'name', 'is', 'Jim'], dtype=str) np.char.not_equal(x1,x2)
String information
Q14 计算特定词语出现次数
-
Count the number of “l” in x, element-wise.
-
x = np.array(['Hello', 'my', 'name', 'is', 'Lily'], dtype=str) np.char.count(X,'l')
Q15 找到特定词语出现index
-
Count the lowest index of “l” in x, element-wise.
-
x = np.array(['Hello', 'my', 'name', 'is', 'Lily'], dtype=str) np.char.find(x,'l')
Q16 判断是否纯数字,大小写
-
Q16-1. Check if each element of x is composed of digits only.
-
Q16-2. Check if each element of x is composed of lower case letters only.
-
Q16-3. Check if each element of x is composed of upper case letters only.
-
x = np.array(['Hello', 'I', 'am', '20', 'years', 'old'], dtype=str) digits = np.char.isdigits(x) lower = np.char.islower(x) upper = np.char.isupper(x)
Q17 判断是否某个词语开头
-
Check if each element of x starts with “hi”.
-
x = np.array(['he', 'his', 'him', 'his'], dtype=str) np.char.startwith(x,'hi')
总结
- add用于两个相同形状的字符型数组相加
- multiply用于重复元素
- capitalize用于首字母大写,title用于所有单词首字母大写,upper都大写,lower都小写,swapcase大小写变换
- center居中,ljust左对齐,rjust右对齐
- encode编码,decode解码
- join插入指定字符串
- strip去除头尾指定字符串,lstrip,rstrip分别去除左右
- split根据特征字符串做元素切割
- splitlines利用换行符换行
- zfill为元素布满0
- replace替换,参数顺序:数组,被替换,替换
- equal和not_equal判断相不相等
- count用于计数出现次数
- find用于找出特征串首次出现索引
- isdigits,islower,isupper判断是否符合数字,全小写,全大写
指定字符串 - strip去除头尾指定字符串,lstrip,rstrip分别去除左右
- split根据特征字符串做元素切割
- splitlines利用换行符换行
- zfill为元素布满0
- replace替换,参数顺序:数组,被替换,替换
- equal和not_equal判断相不相等
- count用于计数出现次数
- find用于找出特征串首次出现索引
- isdigits,islower,isupper判断是否符合数字,全小写,全大写
- startwith判断是否是特征串开头