{ "cells": [ { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'他': 3, '你': 2, '天安门': 5, '我': 1, '爱': 4}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1, 2, 3, 4, 5]\n", "b = ['我', '你', '他', '爱', '天安门']\n", "dict(zip(b,a))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = dict(zip(b,a))\n", "c['你']" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 4, 5]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sentence = '我 爱 天安门'\n", "words = sentence.split(' ')\n", "[c[word] for word in words]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'他': 3, '你': 2, '天安门': 5, '尚学堂': 6, '我': 1, '爱': 4}" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c['尚学堂'] = 6\n", "c" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('我', 1)\n", "('你', 2)\n", "('他', 3)\n", "('爱', 4)\n", "('天安门', 5)\n", "('尚学堂', 6)\n" ] } ], "source": [ "for item in c.items():\n", " print(item)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "我 1\n", "你 2\n", "他 3\n", "爱 4\n", "天安门 5\n", "尚学堂 6\n" ] } ], "source": [ "for key in c.keys():\n", " print(key, c[key])" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n" ] } ], "source": [ "for value in c.values():\n", " print(value)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 我 1\n", "1 你 2\n", "2 他 3\n", "3 爱 4\n", "4 天安门 5\n", "5 尚学堂 6\n" ] } ], "source": [ "for i, key in enumerate(c):\n", " print(i, key, c[key])" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 10\n", "1 20\n", "2 30\n" ] } ], "source": [ "aList = [10, 20 , 30]\n", "for i, value in enumerate(aList):\n", " print(i, value)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 5}" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = {1, 2, 3, 5, 5}\n", "a" ] } ], "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.6.3" } }, "nbformat": 4, "nbformat_minor": 2 }