{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# tuple" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = (1)\n", "type(a)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = (1,)\n", "type(a)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "__main__.MyHost" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from collections import namedtuple\n", "\n", "MyHost = namedtuple('MyHost',['host', 'ip'])\n", "MyHost" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MyHost(host='cn', ip='1.2.3.4')" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "MyHost(host='cn', ip='1.2.3.4')" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": true }, "outputs": [], "source": [ "Hosts = [\n", " ('us','2.2.2.2'),\n", " ('hk','3.3.3.3')\n", "]" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[MyHost(host='us', ip='2.2.2.2'), MyHost(host='hk', ip='3.3.3.3')]" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[ MyHost._make(h) for h in Hosts ]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from ast import literal_eval as make_tuple\n", "make_tuple('(1,2)')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# list" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1,]" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 4, 6]" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(map(lambda x: x*2, [1,2,3]))" ] }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['abd', 'ad', 'b']" ] }, "execution_count": 142, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(['ad','abd','b'])" ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['abd', 'ad', 'b']" ] }, "execution_count": 143, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted({'ad','abd','b','b'})" ] }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['live', 'live', 'arp', 'arp', 'strong']" ] }, "execution_count": 145, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a1 = [\"live\", \"arp\", \"strong\"] \n", "a2 = [\"lively\", \"alive\", \"harp\", \"sharp\", \"armstrong\"]\n", "[s1 for s1 in a1 for s2 in a2 if s1 in s2]" ] }, { "cell_type": "code", "execution_count": 146, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]" ] }, "execution_count": 146, "metadata": {}, "output_type": "execute_result" } ], "source": [ "students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10),] \n", "sorted(students, key=lambda s : s[2]) # sort by age " ] }, { "cell_type": "code", "execution_count": 151, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2, 1]" ] }, "execution_count": 151, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1,2,3][::-1]" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[{1: 3}, {2: 1}, {3: 1}, {4: 2}]\n" ] }, { "data": { "text/plain": [ "Counter({1: 3, 2: 1, 3: 1, 4: 2})" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lst1 = [1, 2, 3, 4, 1, 4, 1]\n", "print( [ {i: lst1.count(i)} for i in set(lst1) ] )\n", "\n", "from collections import Counter\n", "c = Counter(lst1)\n", "c" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(1, 3), (4, 2), (2, 1), (3, 1)]" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c.most_common()" ] }, { "cell_type": "code", "execution_count": 153, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[20, 37, 21]" ] }, "execution_count": 153, "metadata": {}, "output_type": "execute_result" } ], "source": [ "delete_nth = lambda order, max_e: [x for i, x in enumerate(order) if order[:i].count(x) < max_e]\n", "delete_nth([20,37,20,21],1) #item appears at most 1 times" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 3, -1, 2, 1]\n", "[1, 2, -1, 3, 2]\n", "[1, 3, 2, 1]\n" ] }, { "data": { "text/plain": [ "[1, 2, -1, 3, 2, 1, 3, 2, 1]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1,2,3,2,1]\n", "a.remove(1) #insert, remove or sort only modify the list have no return value printed\n", "a.insert(2, -1) # 2 is index, -1 is value\n", "b = [1,2,3,2,1]\n", "b.pop(1) # 1 is index\n", "print(a)\n", "a.reverse()\n", "print(a)\n", "print(b)\n", "a+b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# set" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0, 1, 2, 3, 4, 5, 6, 7}" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{x for x in range(8)} # set" ] }, { "cell_type": "code", "execution_count": 147, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{2, 3}" ] }, "execution_count": 147, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{1,3,2} - {1}" ] }, { "cell_type": "code", "execution_count": 148, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1}" ] }, "execution_count": 148, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{1,3,2} & {1}" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4}" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{1,3,2} | {1, 4}" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 0 1 1\n" ] }, { "data": { "text/plain": [ "{2, 3, 4}" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(1 ^ 1, 0 ^ 0, 1 ^ 0, 0 ^ 1)\n", "\n", "{1,3,2} ^ {1, 4} # not in both" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# dict" ] }, { "cell_type": "code", "execution_count": 149, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'he': 55, 'i': 31, 'she': 44, 'u': 21}" ] }, "execution_count": 149, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = {'i': 30,'u': 20,'she':44}\n", "b = {'i': 31,'u': 21,'he': 55}\n", "c = a.copy()\n", "c.update(b)\n", "c" ] }, { "cell_type": "code", "execution_count": 154, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "defaultdict(, {1: [4, 5], 2: []})" ] }, "execution_count": 154, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from collections import defaultdict\n", "d = defaultdict(list)\n", "d[1] = 3 # int, not default list\n", "d[1] = [4]\n", "d[1].append(5)\n", "d[2] #set to default []\n", "d" ] }, { "cell_type": "code", "execution_count": 155, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 155, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i = defaultdict(int)\n", "i[1]" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: 'a', 2: 'b', 3: 'c'}" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from collections import OrderedDict\n", "d = {1:'a', 3:'c', 2:\"b\"}\n", "d" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "OrderedDict([(1, 'a'), (3, 'c'), (2, 'b')])" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "OrderedDict(d)" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'c'" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[3]" ] } ], "metadata": { "anaconda-cloud": {}, "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.6.3" } }, "nbformat": 4, "nbformat_minor": 1 }