{ "cells": [ { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "#定义模块1函数\n", "def readNList(filename):\n", " f = open(filename)\n", " dL = []\n", " for line in f:\n", " line = line.replace(\"\\n\", \"\")\n", " L = line.split(\",\")\n", " for i in range(len(L)):\n", " L[i] = int(L[i])\n", " dL.append(L)\n", " f.close()\n", " return dL" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[4, 2, 1, 3, 4, 3, 4, 4, 3, 5, 6, 6, 7, 6, 6, 3, 3, 5, 9, 10, 6, 6, 7, 5, 6, 5, 6, 10, 11, 13, 13], [9, 10, 10, 8, 6, 2, 2, 2, 4, 5, 5, 6, 7, 7, 7, 9, 7, 2, 4, 7, 9, 8, 10, 12, 13, 10, 13, 9], [4, 2, 1, 3, 4, 3, 4, 4, 3, 5, 6, 6, 7, 6, 6, 3, 3, 5, 9, 10, 6, 6, 7, 5, 6, 5, 6, 10, 11, 13, 13], [9, 10, 10, 8, 6, 2, 2, 2, 4, 5, 5, 6, 7, 7, 7, 9, 7, 2, 4, 7, 9, 8, 10, 12, 13, 10, 13, 9], [4, 2, 1, 3, 4, 3, 4, 4, 3, 5, 6, 6, 7, 6, 6, 3, 3, 5, 9, 10, 6, 6, 7, 5, 6, 5, 6, 10, 11, 13, 13], [9, 10, 10, 8, 6, 2, 2, 2, 4, 5, 5, 6, 7, 7, 7, 9, 7, 2, 4, 7, 9, 8, 10, 12, 13, 10, 13, 9], [4, 2, 1, 3, 4, 3, 4, 4, 3, 5, 6, 6, 7, 6, 6, 3, 3, 5, 9, 10, 6, 6, 7, 5, 6, 5, 6, 10, 11, 13, 13], [9, 10, 10, 8, 6, 2, 2, 2, 4, 5, 5, 6, 7, 7, 7, 9, 7, 2, 4, 7, 9, 8, 10, 12, 13, 10, 13, 9], [4, 2, 1, 3, 4, 3, 4, 4, 3, 5, 6, 6, 7, 6, 6, 3, 3, 5, 9, 10, 6, 6, 7, 5, 6, 5, 6, 10, 11, 13, 13], [9, 10, 10, 8, 6, 2, 2, 2, 4, 5, 5, 6, 7, 7, 7, 9, 7, 2, 4, 7, 9, 8, 10, 12, 13, 10, 13, 9], [4, 2, 1, 3, 4, 3, 4, 4, 3, 5, 6, 6, 7, 6, 6, 3, 3, 5, 9, 10, 6, 6, 7, 5, 6, 5, 6, 10, 11, 13, 13], [9, 10, 10, 8, 6, 2, 2, 2, 4, 5, 5, 6, 7, 7, 7, 9, 7, 2, 4, 7, 9, 8, 10, 12, 13, 10, 13, 9]]\n" ] } ], "source": [ "#调用模块1函数\n", "filename = \"2013avgtq.csv\"\n", "tqy = readNList(filename)\n", "print(tqy)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "#定义模块2函数\n", "def get_avgy(dL):\n", " avgy = []\n", " for m in dL:\n", " avg = sum(m) / len(m)\n", " avgy.append(avg)\n", " return avgy" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5.870967741935484, 7.25, 5.870967741935484, 7.25, 5.870967741935484, 7.25, 5.870967741935484, 7.25, 5.870967741935484, 7.25, 5.870967741935484, 7.25]\n" ] } ], "source": [ "#调用模块2函数\n", "avgy = get_avgy(tqy)\n", "print(avgy)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "#定义模块3函数\n", "def display_avgy(avgy):\n", " print(\"各月平均气温:\")\n", " month = 1\n", " for avg in avgy:\n", " print(\"{}月: {:.2}°C\".format(month, avg))\n", " month = month + 1" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "各月平均气温:\n", "1月: 5.9°C\n", "2月: 7.2°C\n", "3月: 5.9°C\n", "4月: 7.2°C\n", "5月: 5.9°C\n", "6月: 7.2°C\n", "7月: 5.9°C\n", "8月: 7.2°C\n", "9月: 5.9°C\n", "10月: 7.2°C\n", "11月: 5.9°C\n", "12月: 7.2°C\n" ] } ], "source": [ "#调用模块3函数\n", "display_avgy(avgy)" ] } ], "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 }