Skip to content

Instantly share code, notes, and snippets.

@Albert-W
Created February 6, 2020 20:51
Show Gist options
  • Save Albert-W/af2b9bd55d83c88f1ebb3cea75749d0d to your computer and use it in GitHub Desktop.
Save Albert-W/af2b9bd55d83c88f1ebb3cea75749d0d to your computer and use it in GitHub Desktop.
Basic/切片用法.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "### 切片用法\n\n模式[start:end:step] "
},
{
"metadata": {},
"cell_type": "markdown",
"source": "#### 读取"
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "l = [1,2,3,4,5,6,7,8]",
"execution_count": 106,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "l[:] # 取全部元素",
"execution_count": 107,
"outputs": [
{
"data": {
"text/plain": "[1, 2, 3, 4, 5, 6, 7, 8]"
},
"execution_count": 107,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "l[::-1] # 逆序",
"execution_count": 108,
"outputs": [
{
"data": {
"text/plain": "[8, 7, 6, 5, 4, 3, 2, 1]"
},
"execution_count": 108,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "l[::2] # 取下标为偶数位的元素",
"execution_count": 109,
"outputs": [
{
"data": {
"text/plain": "[1, 3, 5, 7]"
},
"execution_count": 109,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "l[1::2] # 下标为奇数位",
"execution_count": 110,
"outputs": [
{
"data": {
"text/plain": "[2, 4, 6, 8]"
},
"execution_count": 110,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "l[2:4] # 下标为 [2,4)",
"execution_count": 111,
"outputs": [
{
"data": {
"text/plain": "[3, 4]"
},
"execution_count": 111,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "l[:3] # 取前3位,下标为 [0,3) ",
"execution_count": 112,
"outputs": [
{
"data": {
"text/plain": "[1, 2, 3]"
},
"execution_count": 112,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "l[:100] # 超过数组长度时,尾部截断",
"execution_count": 113,
"outputs": [
{
"data": {
"text/plain": "[1, 2, 3, 4, 5, 6, 7, 8]"
},
"execution_count": 113,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "l[100:0] # 起始位置大于长度时,返回空list ",
"execution_count": 132,
"outputs": [
{
"data": {
"text/plain": "[]"
},
"execution_count": 132,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "l[-3:] # 取后3位,下标为 [n-3,n)",
"execution_count": 114,
"outputs": [
{
"data": {
"text/plain": "[6, 7, 8]"
},
"execution_count": 114,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "l[3:] # 下标为 [3,n)",
"execution_count": 115,
"outputs": [
{
"data": {
"text/plain": "[4, 5, 6, 7, 8]"
},
"execution_count": 115,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "l[:-3] # 下标为 [0, n-3) ",
"execution_count": 116,
"outputs": [
{
"data": {
"text/plain": "[1, 2, 3, 4, 5]"
},
"execution_count": 116,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "l[3] # 取下标为3",
"execution_count": 117,
"outputs": [
{
"data": {
"text/plain": "4"
},
"execution_count": 117,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "l[-3] #l[n-3]=l[5] #取下标为n-3的数",
"execution_count": 118,
"outputs": [
{
"data": {
"text/plain": "6"
},
"execution_count": 118,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "l[-1] # 最后一个数,即下标为n-1的数",
"execution_count": 119,
"outputs": [
{
"data": {
"text/plain": "8"
},
"execution_count": 119,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "l[2::3] # 每隔3取一个数",
"execution_count": 120,
"outputs": [
{
"data": {
"text/plain": "[3, 6]"
},
"execution_count": 120,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "for i in range(3):\n print(l[i:i+3]) # 窗口式读取,每次读3个数,循环3次 ",
"execution_count": 121,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "[1, 2, 3]\n[2, 3, 4]\n[3, 4, 5]\n"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "#### 修改"
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "l[0] = 'a' #替换l[0]\nl ",
"execution_count": 122,
"outputs": [
{
"data": {
"text/plain": "['a', 2, 3, 4, 5, 6, 7, 8]"
},
"execution_count": 122,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "#### 插入"
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "l[3:3] = 'c' # 在l[3]处插入元素\nl ",
"execution_count": 123,
"outputs": [
{
"data": {
"text/plain": "['a', 2, 3, 'c', 4, 5, 6, 7, 8]"
},
"execution_count": 123,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "l[:0]= 'a' # 头部插入\nl ",
"execution_count": 124,
"outputs": [
{
"data": {
"text/plain": "['a', 'a', 2, 3, 'c', 4, 5, 6, 7, 8]"
},
"execution_count": 124,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "l[len(l):0]= 'x' # 尾部插入\nl ",
"execution_count": 125,
"outputs": [
{
"data": {
"text/plain": "['a', 'a', 2, 3, 'c', 4, 5, 6, 7, 8, 'x']"
},
"execution_count": 125,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "#### 替换"
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "l[:4] = ['e','f'] # 替换,前4位替换为2位\nl ",
"execution_count": 126,
"outputs": [
{
"data": {
"text/plain": "['e', 'f', 'c', 4, 5, 6, 7, 8, 'x']"
},
"execution_count": 126,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "l[-3:] = ['x','y','z'] # 最后3位替换为其他3位\nl ",
"execution_count": 127,
"outputs": [
{
"data": {
"text/plain": "['e', 'f', 'c', 4, 5, 6, 'x', 'y', 'z']"
},
"execution_count": 127,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "l[::2] = [0]*5 # 奇数为置0,间隔替换时,长度需要相等 \nl ",
"execution_count": 128,
"outputs": [
{
"data": {
"text/plain": "[0, 'f', 0, 4, 0, 6, 0, 'y', 0]"
},
"execution_count": 128,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "#### 删除"
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "l[:2] = [] #删除前2位元素\nl ",
"execution_count": 129,
"outputs": [
{
"data": {
"text/plain": "[0, 4, 0, 6, 0, 'y', 0]"
},
"execution_count": 129,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "del l[:2] # 删除前2位元素\nl ",
"execution_count": 130,
"outputs": [
{
"data": {
"text/plain": "[0, 6, 0, 'y', 0]"
},
"execution_count": 130,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"cell_type": "code",
"source": "del l[::2] # 删除奇数位元素\nl ",
"execution_count": 131,
"outputs": [
{
"data": {
"text/plain": "[6, 'y']"
},
"execution_count": 131,
"metadata": {},
"output_type": "execute_result"
}
]
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.7.3",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"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": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
},
"gist": {
"id": "",
"data": {
"description": "Basic/切片用法.ipynb",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment