numpy错题整理03——字符串数组处理
文章目录
自我介绍
-
我是深圳大学大三的一名大学生,未来想要从事数据分析的工作
-
从今天开始学习python相关库
-
第一步是学习numpy!!
-
若有错误,欢迎指出
资料来源
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.a