{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"1. 计算日平均气温"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"13.25\n",
|
|
"13.25\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"t_Day = [10, 13, 18, 12]\n",
|
|
"#方法1:使用算术表达式\n",
|
|
"avg = (t_Day[0] + t_Day[1] + t_Day[2] + t_Day[3]) / 4\n",
|
|
"print(avg)\n",
|
|
"#方法2:使用内置函数计算\n",
|
|
"avg = sum(t_Day) / len(t_Day)\n",
|
|
"print(avg)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"2. 计算月平均气温"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"31.677419354838708\n",
|
|
"31.677419354838708\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"t_Day = [34,32,33,33,35,36,36,36,35,34,33,32,32,32,31,32,32,30,31,30,30,31,32,29,27,29,31,32,30,26,26] \n",
|
|
"#下标访问方法\n",
|
|
"s = 0\n",
|
|
"n = 0\n",
|
|
"for i in range(len(t_Day)):\n",
|
|
" s = s + t_Day[i]\n",
|
|
" n = n + 1\n",
|
|
"avg = s / n \n",
|
|
"print(avg)\n",
|
|
"#序列迭代访问方法\n",
|
|
"s = 0\n",
|
|
"n = 0\n",
|
|
"for x in t_Day:\n",
|
|
" s = s + x \n",
|
|
" n = n + 1 \n",
|
|
"avg = s / n \n",
|
|
"print(avg)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"3.使用用户自定义函数计算平均值"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"31.677419354838708\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"def getAvg(L):\n",
|
|
" return sum(L) / len(L)\n",
|
|
"avg = getAvg(t_Day)\n",
|
|
"print(avg)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"4.读取文件中的数据到列表"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"['34\\n', '32\\n', '33\\n', '33\\n', '35\\n', '36\\n', '36\\n', '36\\n', '35\\n', '34\\n', '33\\n', '32\\n', '32\\n', '32\\n', '31\\n', '32\\n', '32\\n', '30\\n', '31\\n', '30\\n', '30\\n', '31\\n', '32\\n', '29\\n', '27\\n', '29\\n', '31\\n', '32\\n', '30\\n', '26\\n', '26']\n",
|
|
"[34, 32, 33, 33, 35, 36, 36, 36, 35, 34, 33, 32, 32, 32, 31, 32, 32, 30, 31, 30, 30, 31, 32, 29, 27, 29, 31, 32, 30, 26, 26]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"L = list(open(\"201308tq.txt\"))\n",
|
|
"print(L)\n",
|
|
"for i in range(len(L)):\n",
|
|
" L[i] = int(L[i])\n",
|
|
"print(L)"
|
|
]
|
|
},
|
|
{
|
|
"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.3"
|
|
},
|
|
"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
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|