{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "start_time": "2023-05-04T09:29:48.650911Z", "end_time": "2023-05-04T09:29:49.231339Z" } }, "outputs": [], "source": [ "from IPython.core.interactiveshell import InteractiveShell #执行该代码可以使得当前nb支持多输出\n", "InteractiveShell.ast_node_interactivity = \"all\" \n", "import numpy as np\n", "import pandas as pd \n", "import re " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**作业1**\n", "\n", "利用pandas读入csv数据,求化合物的分子式和质量" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "start_time": "2023-05-04T09:31:30.790867Z", "end_time": "2023-05-04T09:31:34.398474Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The compand is composed of : ['Calcium', 'Carbon', 'Oxygen']\n", " symbol num name atomicMass\n", "0 Ca 1 Calcium 40.078\n", "1 C 1 Carbon 12.011\n", "2 O 3 Oxygen 15.999\n", "The atomic mass of the compound is 100.0869\n" ] } ], "source": [ "# 元素周期表文件,给出任意一个化学表达式,计算其分子元素和质量。输出结果(不得再用C-O2作为输入):\n", "\n", "import csv\n", "def readTable(fileObj):\n", " # 使用pandas reader读取文件对象\n", " df = pd.read_csv(fileObj, index_col='symbol')\n", " return df\n", " \n", "def parserElement(elementStr, table):\n", " # 用 split 方法分割'-'成为数组\n", " elements = elementStr.split('-')\n", " # 初始化函数作用域内变量\n", " names = []\n", " atomicMass_lst = []\n", " symbols = []\n", " numbers = []\n", " mass = 0\n", " # 遍历数组,找寻元素\n", " for element in elements:\n", " # 用正则表达式分离字母与数字 用join把列表连接\n", " number = ''.join(re.findall(r'\\d+', element))\n", " symbol = ''.join(re.findall(r'[^\\d]+', element))\n", " symbols.append(symbol)\n", " # 将元素名称加入数组\n", " names.append(table.loc[symbol]['name'])\n", " # 索引出质量\n", " atomicMass = eval(re.sub(r'\\([^)]*\\)', '', table.loc[symbol]['atomicMass']))\n", " atomicMass_lst.append(atomicMass)\n", " # 判断数量是否为1\n", " if number:\n", " number = int(number)\n", " else:\n", " number = 1\n", " numbers.append(number)\n", " mass += atomicMass * number\n", " # 新建表格\n", " df = pd.DataFrame()\n", " df['symbol'] = symbols\n", " df['num'] = numbers\n", " df['name'] = names\n", " df['atomicMass'] = atomicMass_lst\n", "\n", " print('The compand is composed of : ', names)\n", " print(df)\n", "\n", " print('The atomic mass of the compound is ', mass)\n", "\n", "fileHandle = open('periodic-table.csv','r')\n", "periodicTable = readTable(fileHandle)\n", "\n", "compountString = input('Input a chemical compound, hyphenated,eg. C-O2:')\n", "parserElement(compountString, periodicTable)\n", "\n", "fileHandle.close()" ] }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [], "metadata": { "collapsed": false } } ], "metadata": { "hide_input": false, "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.8.5" }, "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 } }, "nbformat": 4, "nbformat_minor": 2 }