{ "cells": [ { "cell_type": "markdown", "metadata": { "toc": true }, "source": [ "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n", "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#字符串函数\" data-toc-modified-id=\"字符串函数-1\"><span class=\"toc-item-num\">1 </span>字符串函数</a></span><ul class=\"toc-item\"><li><span><a href=\"#2个类似字符串元素的函数\" data-toc-modified-id=\"2个类似字符串元素的函数-1.1\"><span class=\"toc-item-num\">1.1 </span>2个类似字符串元素的函数</a></span><ul class=\"toc-item\"><li><span><a href=\"#np.char.add(a,-b)字符串的拼接,类似相对于字符串的+运算\" data-toc-modified-id=\"np.char.add(a,-b)字符串的拼接,类似相对于字符串的+运算-1.1.1\"><span class=\"toc-item-num\">1.1.1 </span>np.char.add(a, b)字符串的拼接,类似相对于字符串的+运算</a></span></li><li><span><a href=\"#np.char.multiply(a,-i)字符串的重复,-类似字符串的*运算\" data-toc-modified-id=\"np.char.multiply(a,-i)字符串的重复,-类似字符串的*运算-1.1.2\"><span class=\"toc-item-num\">1.1.2 </span>np.char.multiply(a, i)字符串的重复, 类似字符串的*运算</a></span></li></ul></li><li><span><a href=\"#3个类似字符串检索的函数\" data-toc-modified-id=\"3个类似字符串检索的函数-1.2\"><span class=\"toc-item-num\">1.2 </span>3个类似字符串检索的函数</a></span><ul class=\"toc-item\"><li><span><a href=\"#计数np.char.count(a,-object,-开始位置,-终止位置)\" data-toc-modified-id=\"计数np.char.count(a,-object,-开始位置,-终止位置)-1.2.1\"><span class=\"toc-item-num\">1.2.1 </span>计数np.char.count(a, object, 开始位置, 终止位置)</a></span></li><li><span><a href=\"#查找np.char.find(a,-object,-开始位置,-终止位置)\" data-toc-modified-id=\"查找np.char.find(a,-object,-开始位置,-终止位置)-1.2.2\"><span class=\"toc-item-num\">1.2.2 </span>查找np.char.find(a, object, 开始位置, 终止位置)</a></span></li><li><span><a href=\"#索引np.char.index(a,-object,-开始位置,-终止位置)\" data-toc-modified-id=\"索引np.char.index(a,-object,-开始位置,-终止位置)-1.2.3\"><span class=\"toc-item-num\">1.2.3 </span>索引np.char.index(a, object, 开始位置, 终止位置)</a></span></li></ul></li><li><span><a href=\"#三个分割的方法\" data-toc-modified-id=\"三个分割的方法-1.3\"><span class=\"toc-item-num\">1.3 </span>三个分割的方法</a></span><ul class=\"toc-item\"><li><span><a href=\"#np.char.split(a,-分割符,-分割次数)\" data-toc-modified-id=\"np.char.split(a,-分割符,-分割次数)-1.3.1\"><span class=\"toc-item-num\">1.3.1 </span>np.char.split(a, 分割符, 分割次数)</a></span></li><li><span><a href=\"#np.char.splitlines(a,-keepends=None)\" data-toc-modified-id=\"np.char.splitlines(a,-keepends=None)-1.3.2\"><span class=\"toc-item-num\">1.3.2 </span>np.char.splitlines(a, keepends=None)</a></span></li><li><span><a href=\"#np.char.partition(a,-分隔符)\" data-toc-modified-id=\"np.char.partition(a,-分隔符)-1.3.3\"><span class=\"toc-item-num\">1.3.3 </span>np.char.partition(a, 分隔符)</a></span></li></ul></li><li><span><a href=\"#一个合并的方法\" data-toc-modified-id=\"一个合并的方法-1.4\"><span class=\"toc-item-num\">1.4 </span>一个合并的方法</a></span><ul class=\"toc-item\"><li><span><a href=\"#np.char.join(a,-b)\" data-toc-modified-id=\"np.char.join(a,-b)-1.4.1\"><span class=\"toc-item-num\">1.4.1 </span>np.char.join(a, b)</a></span></li></ul></li><li><span><a href=\"#1个替换的方法\" data-toc-modified-id=\"1个替换的方法-1.5\"><span class=\"toc-item-num\">1.5 </span>1个替换的方法</a></span><ul class=\"toc-item\"><li><span><a href=\"#np.char.replace(a,-被替换部分,-新的部分,-替换次数)\" data-toc-modified-id=\"np.char.replace(a,-被替换部分,-新的部分,-替换次数)-1.5.1\"><span class=\"toc-item-num\">1.5.1 </span>np.char.replace(a, 被替换部分, 新的部分, 替换次数)</a></span></li></ul></li><li><span><a href=\"#1个中心化的方法\" data-toc-modified-id=\"1个中心化的方法-1.6\"><span class=\"toc-item-num\">1.6 </span>1个中心化的方法</a></span><ul class=\"toc-item\"><li><span><a href=\"#np.char.center()\" data-toc-modified-id=\"np.char.center()-1.6.1\"><span class=\"toc-item-num\">1.6.1 </span>np.char.center()</a></span></li></ul></li><li><span><a href=\"#2种字符串大小写转换\" data-toc-modified-id=\"2种字符串大小写转换-1.7\"><span class=\"toc-item-num\">1.7 </span>2种字符串大小写转换</a></span><ul class=\"toc-item\"><li><span><a href=\"#np.char.lower(a)\" data-toc-modified-id=\"np.char.lower(a)-1.7.1\"><span class=\"toc-item-num\">1.7.1 </span>np.char.lower(a)</a></span></li><li><span><a href=\"#np.char.upper(a)\" data-toc-modified-id=\"np.char.upper(a)-1.7.2\"><span class=\"toc-item-num\">1.7.2 </span>np.char.upper(a)</a></span></li></ul></li><li><span><a href=\"#1中去除字符串两端的方法\" data-toc-modified-id=\"1中去除字符串两端的方法-1.8\"><span class=\"toc-item-num\">1.8 </span>1中去除字符串两端的方法</a></span></li></ul></li></ul></div>" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "#全部行都能输出\n", "from IPython.core.interactiveshell import InteractiveShell\n", "InteractiveShell.ast_node_interactivity = \"all\"" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 字符串函数" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "基本和python内置的字符串方法一样" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2个类似字符串元素的函数" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### np.char.add(a, b)字符串的拼接,类似相对于字符串的+运算\n", "a b 是两个ndarray对象, 这两个对象的元素都必须全是字符串,其作用是把对应位置的字符串进行加法运算。 \n", "要求是a b的形状相同, 或者满足广播规则!" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['123', '456', '789'], dtype='<U3')" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "array(['asd', 'fgh', 'jkl'], dtype='<U3')" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array(['123', '456', '789'])\n", "b = np.array(['asd', 'fgh', 'jkl'])\n", "a\n", "b" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['123asd', '456fgh', '789jkl'], dtype='<U6')" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.char.add(a, b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "但是由于如果只含有bool型, 数值型, 字符串作为元素的时候, 会自动全部转化为字符串,所以也是可以的。" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['True', '123', '12.45', '999'], dtype='<U32')" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "array(['A', 'B', 'C', 'D'], dtype='<U1')" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array([True, 123, 12.45, '999']) # 会全部转化为字符串\n", "b = np.array(['A', 'B', 'C', 'D'])\n", "a\n", "b" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['TrueA', '123B', '12.45C', '999D'], dtype='<U33')" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.char.add(a, b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### np.char.multiply(a, i)字符串的重复, 类似字符串的*运算\n", "a是ndarray对象, 并且,a中的元素全部是字符串,i是一个整数。" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['123', '456', '789', '12'], dtype='<U3')" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array(['123', '456', '789', 12])\n", "a" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['123123123123123123', '456456456456456456', '789789789789789789',\n", " '121212121212'], dtype='<U18')" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.char.multiply(a, 6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3个类似字符串检索的函数" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 计数np.char.count(a, object, 开始位置, 终止位置)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['112,1', 'asda1', 'asd', '232'], dtype='<U5')" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array(['112,1', 'asda1', 'asd', 232])\n", "a" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([3, 1, 0, 0])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.char.count(a, '1')" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 1, 0, 0])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.char.count(a, '1', 1) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 查找np.char.find(a, object, 开始位置, 终止位置)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['112,1', 'asda1', 'asd', '232'], dtype='<U5')" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array(['112,1', 'asda1', 'asd', 232])\n", "a" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0, 4, -1, -1])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.char.find(a, '1') # 返回第一次出出现的索引 ,如果找不到返回-1" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 4, 4, -1, -1])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.char.find(a, '1', 2) # 在指定范围内查找, 如果找到就返回索引, 找不到返回-1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 索引np.char.index(a, object, 开始位置, 终止位置)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['112,a1', '1asda1', '11asd', 'a232'], dtype='<U6')" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array(['112,a1', '1asda1', '11asd', 'a232'])\n", "a" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([4, 1, 2, 0])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.char.index(a, 'a') # 返回第一次出现的索引" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "substring not found", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m<ipython-input-19-6e54c96fdcbf>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mchar\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'1'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 找不到就报错\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;32mD:\\Anaconda3\\lib\\site-packages\\numpy\\core\\defchararray.py\u001b[0m in \u001b[0;36mindex\u001b[1;34m(a, sub, start, end)\u001b[0m\n\u001b[0;32m 752\u001b[0m \"\"\"\n\u001b[0;32m 753\u001b[0m return _vec_string(\n\u001b[1;32m--> 754\u001b[1;33m a, integer, 'index', [sub, start] + _clean_args(end))\n\u001b[0m\u001b[0;32m 755\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 756\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mValueError\u001b[0m: substring not found" ] } ], "source": [ "np.char.index(a, '1') # 找不到就报错" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 三个分割的方法" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### np.char.split(a, 分割符, 分割次数)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['112,a1', '1asda1', '11asd', '1a232'], dtype='<U6')" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array(['112,a1', '1asda1', '11asd', '1a232'])\n", "a" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([list(['112,', '1']), list(['1', 'sd', '1']), list(['11', 'sd']),\n", " list(['1', '232'])], dtype=object)" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.char.split(a, 'a') # 第三个参数不写默认全部分割, 返回的结果是以列表的形式存储" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([list(['112,', '1']), list(['1', 'sda1']), list(['11', 'sd']),\n", " list(['1', '232'])], dtype=object)" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.char.split(a, 'a', 1) # 只分割一次" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### np.char.splitlines(a, keepends=None)\n", "分隔符只能是\\r \\n \\r\\n" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['112\\n,a1', '1as\\tda1', '11a\\rsd', '1a2\\r\\n3 2'], dtype='<U8')" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array(['112\\n,a1', '1as\\tda1', '11a\\rsd', '1a2\\r\\n3 2'])\n", "a" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([list(['112', ',a1']), list(['1as\\tda1']), list(['11a', 'sd']),\n", " list(['1a2', '3 2'])], dtype=object)" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.char.splitlines(a, keepends=None)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([list(['112\\n', ',a1']), list(['1as\\tda1']), list(['11a\\r', 'sd']),\n", " list(['1a2\\r\\n', '3 2'])], dtype=object)" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.char.splitlines(a, keepends=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### np.char.partition(a, 分隔符)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['122aa12', '232as', '1a1asas'], dtype='<U7')" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array(['122aa12', '232as', '1a1asas'])\n", "a" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([['122', 'a', 'a12'],\n", " ['232', 'a', 's'],\n", " ['1', 'a', '1asas']], dtype='<U5')" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.char.partition(a, 'a') # 返回的是一个二维数组, 保留分隔符" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 一个合并的方法" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### np.char.join(a, b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "相当于对应位置上的字符串进行join方法" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['+', '-'], dtype='<U1')" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array(['+', '-'])\n", "a" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['1234', '5678'], dtype='<U4')" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = np.array(['1234', '5678'])\n", "b" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['1+2+3+4', '5-6-7-8'], dtype='<U7')" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.char.join(a, b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1个替换的方法" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### np.char.replace(a, 被替换部分, 新的部分, 替换次数)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['asdasd', 'asdqweasd', 'asddsfasdaaa'], dtype='<U12')" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array(['asdasd', 'asdqweasd', 'asddsfasdaaa'])\n", "a" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['AsdAsd', 'AsdqweAsd', 'AsddsfAsdAAA'], dtype='<U12')" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.char.replace(a, 'a', 'A') # 默认全部替换" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['Asdasd', 'Asdqweasd', 'Asddsfasdaaa'], dtype='<U12')" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.char.replace(a, 'a', 'A', 1) # 只替换一次" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1个中心化的方法 \n", "### np.char.center()\n", "将每一个字符串放中间,其余用特定的符号补齐" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['a', 'b', 'c', 'python'], dtype='<U6')" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array(['a', 'b', 'c', 'python'])\n", "a" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['*************************************************a**************************************************',\n", " '*************************************************b**************************************************',\n", " '*************************************************c**************************************************',\n", " '***********************************************python***********************************************'],\n", " dtype='<U100')" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.char.center(a, 100, '*')" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([' a ',\n", " ' b ',\n", " ' c ',\n", " ' python '],\n", " dtype='<U100')" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.char.center(a, 100) # 不指定符号就默认用空格补齐" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2种字符串大小写转换" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### np.char.lower(a) \n", "将a中的每一个大写字符转化为小写" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['asASd', 'fSDAgh', 'jSADkl'], dtype='<U6')" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array(['asASd', 'fSDAgh', 'jSADkl'])\n", "a" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['asasd', 'fsdagh', 'jsadkl'], dtype='<U6')" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.char.lower(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### np.char.upper(a) \n", "将a中的每一个每一个小写字符转化为大写" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['asdaASDAS', 'asdaSADAASDads', 'asd'], dtype='<U14')" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array(['asdaASDAS', 'asdaSADAASDads', 'asd'])\n", "a" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['ASDAASDAS', 'ASDASADAASDADS', 'ASD'], dtype='<U14')" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.char.upper(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1中去除字符串两端的方法" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['1232311', 'qweqwe1', 'qweq123'], dtype='<U7')" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array(['1232311', 'qweqwe1', 'qweq123'])\n", "a" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['2323', 'qweqwe', 'qweq123'], dtype='<U7')" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.char.strip(a, '1') " ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([' as\\t', '\\nasxa '], dtype='<U7')" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array([' as\\t', '\\nasxa '])\n", "a" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['as', 'asxa'], dtype='<U7')" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.char.strip(a) # 第二个参数不填默认去除两端的空格 \\t \\n等特殊字符" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['1232311', 'qweqwe1', 'qweq123'], dtype='<U7')" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array(['1232311', 'qweqwe1', 'qweq123'])\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": true, "toc_position": { "height": "calc(100% - 180px)", "left": "10px", "top": "150px", "width": "349.167px" }, "toc_section_display": true, "toc_window_display": true } }, "nbformat": 4, "nbformat_minor": 2 }