{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 《Data Master》\n", ">#### 【人人都是数据科学家】,by [openthings@163.com](http://my.oschina.net/u/2306127/blog?catalog=3420733), 2016-04. \n", "\n", "## 基础篇—Python快速入门\n", "\n", ">### (一)列表、词典、元组及其遍历。\n", "##### Array,数组由一系列数据组成。在Python中有list、dictionary、Tuple等多种实现。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List,使用[a,b,c,...]方式声明。 \n", "列表是基础的数据结构。" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "总计: 5\n", "元素: [0, 1, 2, 3, 4]\n" ] } ], "source": [ "alist = [0,1,2,3,4]\n", "print(\"总计:\",len(alist))\n", "print(\"元素:\", alist)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "字符串的列表。" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "red\n", "green\n", "blue\n" ] } ], "source": [ "colours = [\"red\",\"green\",\"blue\"]\n", "for colour in colours:\n", " print(colour)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "列表的传统方式遍历。" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for i in range(0,len(alist)):\n", " print(alist[i])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "列表的递归方式遍历。" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for i in alist:\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可以直接调用一个列表。" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for obj in [0,1,2,3,4]:\n", " print(obj)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 创建一个矩阵。" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[11, 12, 13]\n", "[21, 22, 23]\n", "[31, 32, 33]\n" ] } ], "source": [ "olist = [[11,12,13],[21,22,23],[31,32,33]]\n", "for row in olist:\n", " print(row)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 生成一个数据序列。在做数值检验时很有用。" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for obj in range(5):\n", " print(obj)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 生成一个数据序列:range(开始值,结束值,步长)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "7\n", "9\n" ] } ], "source": [ "for obj in range(5,10,2):\n", " print(obj)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### String as a list of char. 字符串是字符数组。" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(0) B\n", "(1) e\n", "(2) g\n", "(3) i\n", "(4) n\n", "(5) M\n", "(6) a\n", "(7) n\n" ] } ], "source": [ "name='BeginMan'\n", "for obj in range(len(name)):\n", " print('(%d)' %obj,name[obj])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionary,词典,{key:value,...}\n", "词典数据就是一系列k:v值对的集合。" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Dict Length: 3\n", "{'age': 22, 'job': 'pythoner', 'name': 'BeginMan'}\n" ] } ], "source": [ "dict = {'name':'BeginMan','job':'pythoner','age':22}\n", "\n", "print(\"Dict Length: \",len(dict))\n", "print(dict)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "_**注意:**上面的词典数据的输出与json表示是完全一致的,后面在json会专门介绍。_\n", "\n", "词典的遍历:" ] }, { "cell_type": "code", "execution_count": 71, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'BeginMan'" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dict[\"name\"]" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Key \t Value\n", "=================\n", "age \t BeginMan\n", "job \t BeginMan\n", "name \t BeginMan\n" ] } ], "source": [ "print(\"Key\",\"\\t Value\")\n", "print(\"=================\")\n", "for key in dict:\n", " print(key,\"\\t\",dict[i])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "dict的每一个item(obj)是一个二元组(下面介绍元组)。" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('age', 22)\n", "('job', 'pythoner')\n", "('name', 'BeginMan')\n" ] } ], "source": [ "for obj in dict.items():\n", " print(obj)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "age 22\n", "job pythoner\n", "name BeginMan\n" ] } ], "source": [ "for k,v in dict.items():\n", " print(k,v)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Json的简单演示,将dict转为典型的json字符串表示。" ] }, { "cell_type": "code", "execution_count": 72, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'{\"age\": 22, \"job\": \"pythoner\", \"name\": \"BeginMan\"}'\n" ] } ], "source": [ "import json\n", "\n", "j = json.dumps(dict)\n", "print(repr(j))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 从JSON字符串载入一个dict对象。" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Type of d: \n", "{'name': 'BeginMan', 'job': 'pythoner', 'age': 22}\n" ] } ], "source": [ "d = json.loads('{\"age\": 22, \"job\": \"pythoner\", \"name\": \"BeginMan\"}')\n", "print(\"Type of d: \", type(d))\n", "print(d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## tuple,(obj1,obj2,...),元组\n", "一个元组可包含多种类型的对象,不可修改。" ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "collapsed": false }, "outputs": [], "source": [ "tup = 'abc',1,2,'x',True" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(5, ('abc', 1, 2, 'x', True))" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(tup),tup" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 元组的赋值和取值。" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x,y =1,2" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(1, 2)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x,y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 一个字典、元组构成的复合列表对象。" ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[{'k1': 'key', 'k2': 2}, (3, 'element')]" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ao = [{\"k1\":\"key\",\"k2\":2},(3,\"element\")]\n", "ao" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 访问这个符合对象的值。" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "len(ao),ao[0][\"k1\"],ao[1][0],ao[1][1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "从上面可以看出,python的数据结构是非常灵活的,是数据探索和分析、处理的利器。" ] } ], "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.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }