{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Словари" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "неупорядоченная коллекция\n", "\n", "ассоциативный массив или хеш\n", "\n", "неупорядоченное множество пар ключ: значение с требованием уникальности ключей\n", "\n", "{} - создание пустого словаря\n", "\n", "\n", "Основные особенности словарей:\n", "\n", "1.\tДоступ осуществляется по ключу, а не по индексу. \n", "2.\tЗначения хранятся в неотсортированном порядке.\n", "3.\tСловарь может хранить вложенные словари, в качестве значений объекты любого типа. Ключ в словаре может быть строкой, целым или вещественным числом либо кортежем, состоящим из указанных типов. Ключ не допускает изменений.\n", "4.\tСловари реализованы как хеш-таблицы с быстрым доступом.\n", "5.\tСловари как и списки хранят ссылки на объекты, а не сами объекты.\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Ivan': 23, 'Vasya': 19, 'Olya': 18}\n", "23\n", "{'Vasya': 19, 'Olya': 18}\n", "dict_keys(['Vasya', 'Olya'])\n", "True\n", "False\n" ] } ], "source": [ "dic = {'Ivan' : 23, 'Vasya' : 18}\n", "dic['Vasya'] = 19\n", "dic['Olya'] = 18\n", "print(dic)\n", "print(dic['Ivan'])\n", "del dic['Ivan']\n", "print(dic)\n", "print(dic.keys())\n", "print('Olya' in dic)\n", "print(18 in dic)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Создание словаря" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'name': 'Ivan', 'age': 25}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "D = {'name': 'Ivan', 'age': 25}\n", "D" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'name': 'Egor', 'age': 15}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "D = {}\n", "D['name'] = 'Egor'\n", "D['age'] = 15\n", "D" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'id': 1999, 'name': 'Egor', 'size': 3}\n", "{'id': 1999, 'name': 'Egor', 'size': 3}\n", "{'id': 1999, 'name': 'Egor', 'size': 3}\n", "{'id': 1999, 'name': 'Egor', 'size': 3}\n" ] } ], "source": [ "d1 = dict(id = 1999, name = \"Egor\", size = 3)\n", "d2 = dict({\"id\": 1999, \"name\": \"Egor\", \"size\": 3})\n", "d3 = dict([(\"id\", 1999), (\"name\", \"Egor\"), (\"size\", 3)])\n", "d4 = dict(zip((\"id\", \"name\", \"size\"), (1999, \"Egor\", 3)))\n", "print(d1)\n", "print(d2)\n", "print(d3)\n", "print(d4)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'name': None, 'age': None}\n", "{'name': 123, 'age': 123}\n" ] } ], "source": [ "d1 = {}.fromkeys(['name', 'age'])\n", "d2 = {}.fromkeys(['name', 'age'], 123)\n", "print(d1)\n", "print(d2)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = dict((x, x**2) for x in range(5))\n", "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Методы работы со словарем" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}\n", "{}\n" ] } ], "source": [ "print(len(d))\n", "print(d)\n", "d.clear()\n", "print(d)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Alice phone is 2341\n" ] } ], "source": [ "people = {'Alice': {'phone': '2341', 'addr': 'Lenin st, 23'},\n", " 'Alik': {'phone': '1311', 'addr': 'Lenin st, 13'},\n", " 'Mike': {'phone': '9102', 'addr': 'Maklin st, 42'}}\n", "name = 'Alice' \n", "key = 'phone'\n", "if name in people: \n", " print(\"%s phone is %s\" % (name, people[name][key]))\n" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'user': 'admin', 'attr': [1, 2, 3]}\n", "{'user': 'admin', 'attr': [2, 3]}\n", "{'user': 'admin', 'attr': [2, 3]}\n", "True\n", "{'user': 'admin', 'attr': [2, 3], 'new': 34}\n", "{'user': 'admin', 'attr': [2, 3]}\n", "False\n" ] } ], "source": [ "x = {\"user\": 'admin','attr': [1, 2, 3]}\n", "y = x.copy()\n", "print(y)\n", "x['attr'].remove(1)\n", "print(x)\n", "print(y)\n", "print(x==y)\n", "x['new']=34\n", "print(x)\n", "print(y)\n", "print(x==y)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'user': 'admin', 'attr': [1, 2, 3]}\n", "{'user': 'admin', 'attr': [1, 2, 3]}\n", "{'user': 'admin', 'attr': [1, 2, 3]}\n", "{'user': 'admin', 'attr': [2, 3]}\n", "False\n" ] } ], "source": [ "from copy import deepcopy\n", "x = {\"user\": 'admin', 'attr': [1, 2, 3]}\n", "print(x)\n", "z = deepcopy(x)\n", "print(z)\n", "z['attr'].remove(1)\n", "print(x)\n", "print(z)\n", "print(x==z)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "admin\n", "None\n" ] }, { "ename": "KeyError", "evalue": "'name'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0md\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0md\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'name'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0md\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'name'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mKeyError\u001b[0m: 'name'" ] } ], "source": [ "print(x.get('user'))\n", "d = {}\n", "print(d.get('name'))\n", "print(d['name']) #error" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Alice {'phone': '2341', 'addr': 'Lenin st, 23'}\n", "Alik {'phone': '1311', 'addr': 'Lenin st, 13'}\n", "Mike {'phone': '9102', 'addr': 'Maklin st, 42'}\n", "{'Alice': {'phone': '2341', 'addr': 'Lenin st, 23'}, 'Alik': {'phone': '1311', 'addr': 'Lenin st, 13'}, 'Mike': {'phone': '9102', 'addr': 'Maklin st, 42'}}\n" ] } ], "source": [ "for key, value in people.items():\n", " print(key, value)\n", "print((people))" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dict_keys(['Alice', 'Alik', 'Mike'])\n", "dict_values([{'phone': '2341', 'addr': 'Lenin st, 23'}, {'phone': '1311', 'addr': 'Lenin st, 13'}, {'phone': '9102', 'addr': 'Maklin st, 42'}])\n" ] } ], "source": [ "print(people.keys())\n", "print(people.values())" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Alice': {'phone': '2341', 'addr': 'Lenin st, 23'}, 'Alik': {'phone': '1311', 'addr': 'Lenin st, 13'}, 'Mike': {'phone': '9102', 'addr': 'Maklin st, 42'}}\n", "{'Alice': {'phone': '2341', 'addr': 'Lenin st, 23'}, 'Alik': {'phone': '1311', 'addr': 'Lenin st, 13'}}\n" ] } ], "source": [ "print(people)\n", "people.pop('Mike')\n", "print(people)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('Alik', {'phone': '1311', 'addr': 'Lenin st, 13'})\n", "{'Alice': {'phone': '2341', 'addr': 'Lenin st, 23'}}\n" ] } ], "source": [ "print(people.popitem())\n", "print(people)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'title': 'Python Web Site', 'url': 'http://www.python.org', 'www': 'python'}\n", "{'title': 'Python Web Site', 'url': 'http://www.python.org', 'www': 'python.org'}\n" ] } ], "source": [ "d = {'title': 'Python Web Site', 'url': 'http://www.python.org', 'www': 'python'}\n", "print(d)\n", "d2 = {'www':'python.org'}\n", "d.update(d2)\n", "print(d)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{1: 11, 2: 22, 3: 33}\n", "dict_values([11, 22, 33])\n", "{1: 11, 3: 33}\n" ] }, { "ename": "KeyError", "evalue": "22", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0md\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0md\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m \u001b[1;32mdel\u001b[0m \u001b[0md\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m22\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 10\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0md\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mKeyError\u001b[0m: 22" ] } ], "source": [ "d={}\n", "d[1]=11\n", "d[2]=22\n", "d[3]=33\n", "print(d)\n", "print(d.values())\n", "del d[2]\n", "print(d)\n", "del d[22] # error\n", "print(d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Операции" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python - Guido van Rossum\n", "Perl - Larry Wall\n", "Tcl - John Ousterhout\n" ] } ], "source": [ "table = {'Python': 'Guido van Rossum',\n", " 'Perl': 'Larry Wall',\n", " 'Tcl': 'John Ousterhout' }\n", "for lang in table:\n", " print(lang, '-', table[lang])\n" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{(2, 3, 4): 88, (7, 8, 9): 99}\n", "88\n" ] } ], "source": [ "Matrix = {}\n", "Matrix[(2, 3, 4)] = 88\n", "Matrix[(7, 8, 9)] = 99\n", "X = 2; Y = 3; Z = 4\n", "print(Matrix)\n", "print(Matrix[(X, Y, Z)])" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Serg\n", "writer\n" ] } ], "source": [ "man = {'name': 'Serg',\n", " 'jobs': ['programmer', 'writer'],\n", " 'web': 'www.iakovlev.org',\n", " 'home': {'city': 'Moscow', 'zip': 129000}}\n", "print(man['name'])\n", "print(man['jobs'][1])" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'h': 3, 'o': 1, 'w': 1, ' ': 3, 'm': 2, 'a': 1, 'n': 1, 'y': 1, 't': 1, 'i': 1, 'e': 1, 's': 1}\n" ] } ], "source": [ "def histogram(s):\n", " d = dict()\n", " for c in s:\n", " if c not in d:d[c] = 1\n", " else:d[c] += 1\n", " return d\n", "hist = histogram('how many times hh')\n", "print(hist)\n", "\n" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{3: ['h', ' '], 1: ['o', 'w', 'a', 'n', 'y', 't', 'i', 'e', 's'], 2: ['m']}\n" ] } ], "source": [ "def invert_dict(d):\n", " inv = dict()\n", " for key in d:\n", " val = d[key]\n", " if val not in inv:inv[val] = [key]\n", " else:inv[val].append(key)\n", " return inv\n", "print(invert_dict(hist))" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'hello': 4, 'world': 3, 'the': 1, 'beautiful': 1}\n", "'hello' occurs 4 times\n", "'world' occurs 3 times\n" ] } ], "source": [ "import string\n", "import sys\n", "\n", "words = {}\n", "strip = string.whitespace + string.punctuation + string.digits + \"\\\"'\"\n", "filename = 'my_file.txt'\n", "for line in open(filename):\n", " for word in line.lower().split():\n", " word = word.strip(strip)\n", " if len(word) > 2:\n", " words[word] = words.get(word, 0) + 1\n", "print(words)\n", "for word in sorted(words):\n", " if (words[word]>1): print(\"'{0}' occurs {1} times\".format(word, words[word])) \n" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "awk - Brian Kernighan\n", "java - James Gosling\n", "parrot - Simon Cozens\n", "perl - Larry Wall\n", "php - Rasmus Lerdorf\n", "python - Guido van Rossum\n", "tcl - John Ousterhout\n", "\n", "Brian Kernighan\n", "Guido van Rossum\n", "James Gosling\n", "John Ousterhout\n", "Larry Wall\n", "Rasmus Lerdorf\n", "Simon Cozens\n" ] } ], "source": [ "author = {\"php\":\"Rasmus Lerdorf\",\\\n", " \"perl\":\"Larry Wall\",\\\n", " \"tcl\":\"John Ousterhout\",\\\n", " \"awk\":\"Brian Kernighan\",\\\n", " \"java\":\"James Gosling\",\\\n", " \"parrot\":\"Simon Cozens\",\\\n", " \"python\":\"Guido van Rossum\"}\n", "\n", "for language in sorted(author.keys()):\n", " print(language,\" - \", author[language])\n", "\n", "print()\n", "\n", "for aut in sorted(author.values()):\n", " print(aut)\n" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'parent1': ['child1', 'child2'], 'parent2': ['child3', 'child4']}\n" ] } ], "source": [ "def invert_dict_nonunique(d):\n", " newdict = {}\n", " for k, v in d.items():\n", " newdict.setdefault(v, []).append(k)\n", " return newdict\n", "d = {'child1': 'parent1','child2': 'parent1','child3': 'parent2','child4': 'parent2'}\n", "print(invert_dict_nonunique(d))\n" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4 - ['hello']\n", "3 - ['world']\n", "1 - ['the', 'beautiful']\n" ] } ], "source": [ "newdict = {}\n", "for k, v in words.items():\n", " newdict.setdefault(v, []).append(k) \n", " \n", "for no in sorted(newdict.keys(), reverse = True):\n", " print(no, \" - \", newdict[no]) \n" ] } ], "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.5" } }, "nbformat": 4, "nbformat_minor": 2 }