{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#### STRING" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['SEC', '2018W28', '250']\n", "SEC\n", "201828\n", "['', ' 2018']\n", "['Sec ', '']\n" ] } ], "source": [ "yearweekRaw = \"2018W06\"\n", "\n", "split_test = str(yearweekRaw)\n", "charlie = split_test.split(\"w\")\n", "delta = split_test.split(\"W\")\n", "ChangeForm = (delta)\n", "# print(charlie)\n", "# print(delta)\n", "\n", "testString = \"Sec 2018W28 250\"\n", "testSplit = testString.upper().split(\" \")\n", "print(testSplit)\n", "print(testSplit[0])\n", "print(testSplit[1].replace('W',''))\n", "\n", "\n", "delimiter = testString.index(\"W\")\n", "tString1 = testString[:delimiter].split(\"Sec\")\n", "print(tString1)\n", "\n", "tString2 = testString[:delimiter].split(\"2018\")\n", "print(tString2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### LIST" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### LIST0" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "[1, 2, 3, 4, 5, 6, 7, 8, 9, 'b']\n" ] } ], "source": [ "# List comprehension\n", "test = [i for i in range(1,10)]\n", "\n", "# print(test[0],test[1],test[2])\n", "\n", "print(test.count(1))\n", "\n", "# add str or int into the end of list \n", "test.append(\"b\")\n", "print(test) \n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### LIST 1: INDEX" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b\n", "d\n" ] }, { "data": { "text/plain": [ "['d', 'e']" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "secInfo = [\"a\",\"b\",\"c\",[\"d\",\"e\",\"f\"]]\n", "print(secInfo[1])\n", "print(secInfo[3][0])\n", "\n", "r1 = secInfo[3]\n", "r2 = r1[0:2]\n", "r2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### LIST2: NUMBER COMPARISON" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "414.0\n", "300\n" ] } ], "source": [ "import statistics as stat\n", "\n", "testList = [120,150,300,500,1000,100,2000]\n", "\n", "# Choose minimun and maxium number\n", "MinOut = min(testList)\n", "MaxOut = max(testList)\n", "\n", "# Choose median munber using stat module\n", "MedianNum= stat.median(testList)\n", "\n", "\n", "alpha = testList.index(MinOut)\n", "beta = testList.index(MaxOut)\n", "\n", "# Remove the specific number\n", "testList.pop(testList.index(MinOut))\n", "testList.pop(testList.index(MaxOut)) \n", "\n", "TotalSum = sum(testList)\n", "num = len(testList) \n", "testAvg = TotalSum/num \n", " \n", "print(testAvg)\n", "print(MedianNum) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List update & delete" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[220, 330, 140, 650]\n", "[220, 330, 180, 650]\n" ] } ], "source": [ "updateList = [220,330,140,650]\n", "print(updateList)\n", "updateList[2] = 180\n", "print(updateList)\n", "\n", "functionList.append(100) / functionList.insert(4,100) 3ë²ì§¸ ì¸ë±ì¤ì ì¶ê°" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[220, 330, 180]\n" ] } ], "source": [ "del updateList[3]\n", "print(updateList)\n", "\n", "# 1<=ì¸ë±ì¤<3\n", "# updatelist[1:3] = []\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[100, 200, 400, 500]\n" ] } ], "source": [ "testList = [100,200,300,400,500] \n", "testList.pop(testList.index(300))\n", "print(testList)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### CONVERT LIST TO STRING TO PRINT NEW LINE(ê°í)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SMART\n", "FINANACE\n", "PYTHON\n", "CLASS\n" ] } ], "source": [ "secInfo = [\"SMART\",\"FINANACE\",\"PYTHON\",\"CLASS\"]\n", "print(\"\\n\".join(secInfo))\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### DICNTIONARY" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('12000', '20000')\n", "dict_items([('name', 'job'), ('address', 'suwon'), ('price', ('12000', '20000'))])\n", "name\n", "address\n", "price\n", "job\n", "suwon\n", "('12000', '20000')\n", "name job\n", "address suwon\n", "price ('12000', '20000')\n", "0 name\n", "1 address\n", "2 price\n" ] } ], "source": [ "#///////////////////////////////\n", "testDict = {\"name\":\"job\",\"id\":\"300000\",\"address\":\"suwon\"}\n", "type(testDict)\n", " \n", "del testDict[\"id\"]\n", "\n", "\n", "#///////////////////////////////\n", "\n", "\n", "testDict[\"price\"] = (\"12000\",\"20000\")\n", "print(testDict[\"price\"])\n", "\n", "\n", "#///////////////////////////////\n", "\n", "\n", "kay = testDict.items()\n", "print(kay)\n", "\n", "for k in testDict.keys():\n", " print(k)\n", "\n", "for v in testDict.values():\n", " print(v)\n", "\n", "for k,v in testDict.items():\n", " print(k,v)\n", " \n", " \n", "# ë´ì¥ í¨ì enumerate \n", "for k,v in enumerate(testDict):\n", " print(k,v)\n", "\n", "#///////////////////////////////" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### DICTIONARY: ê°ë³ì ì¸ LIST를 ê²°í©í´ì DICTONARY ë§ë¤ê¸°" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'AGROP': 1, 'BGROUP': 2, 'CGROUP': 3, 'DGROUP': 4, 'EGROUP': 5}\n" ] } ], "source": [ "keyName = [\"AGROP\",\"BGROUP\",\"CGROUP\",\"DGROUP\",\"EGROUP\"]\n", "rank = [(i) for i in range(1,6)]\n", "testDict = dict(zip(keyName,rank)) \n", "print(testDict)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### DICTIONARY í¤ ê°ë§ ì¶ì¶í기" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'name': None, 'id': None, 'address': None}\n" ] } ], "source": [ "testDict = {\"name\":\"job\",\"id\":\"300000\",\"address\":\"suwon\"}\n", "x= {key: value for key, value in dict.fromkeys(testDict).items() }\n", "\n", "print(x)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### PANDASë¡ DICTIONARYì LIST 를 DF" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " age name stock_age\n", "0 27 custA 2\n", "1 40 custB 54\n", "2 33 custC 6\n", "3 29 custD 3\n", "**************************************************\n", " 10**n 2**n\n", "0 10 2\n", "1 100 4\n", "2 1000 8\n", "3 10000 16\n", "**************************************************\n", " age name stock_age 10**n 2**n\n", "0 27 custA 2 10 2\n", "1 40 custB 54 100 4\n", "2 33 custC 6 1000 8\n", "3 29 custD 3 10000 16\n" ] } ], "source": [ "import pandas as pd\n", "# ëì ë리 ìë£íì pandas 모ëë¡ dataframe ìì±\n", "data = { \n", " 'stock_age':[2,54,6,3],\n", " 'name':['custA','custB','custC','custD'],\n", " 'age':[27,40,33,29]\n", " }\n", "\n", "dF = pd.DataFrame(data)\n", "print(dF)\n", "\n", "print(\"*\"*50)\n", "\n", "# 리ì¤í¸ ìë£íì pandas 모ëë¡ dataframe ìì±\n", "numTen = [10,100,1000,10000]\n", "testDf = pd.DataFrame(numTen, columns=['10**n'])\n", "\n", "numTwo = [2,4,8,16]\n", "testDf2 = pd.DataFrame(numTwo, columns=['2**n'])\n", "\n", "# ê° ë¦¬ì¤í¸ ìë£íë¤ì í©ì¹ê¸°\n", "final = pd.concat([testDf,testDf2], axis = 1)\n", "\n", "print(final)\n", "# print(type(final))\n", "\n", "print(\"*\"*50)\n", "\n", "# 리ì¤í¸ ìë£í, ëì ë리 ìë£í í©ì¹ê¸°\n", "finalTwo = pd.concat([dF,testDf,testDf2], axis = 1)\n", "print(finalTwo)\n", "\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "8 ê³±ì \n", "8 * 1 = 8\n", "8 * 2 = 16\n", "8 * 3 = 24\n", "8 * 4 = 32\n", "8 * 5 = 40\n", "8 * 6 = 48\n", "8 * 7 = 56\n", "8 * 8 = 64\n" ] } ], "source": [ "# list ì ì´ì¤ for문 ë³µí© ì¬ì©\n", "\n", "newList = [(i) for i in range(1,10)]\n", "# newList = [1,2,3,4,5,6,7,8,]\n", "# len(리ì¤í¸ ë³ìëª )-1 : 리ì¤í¸ìë£ ìì ë§ì§ë§ ì ë ¥ê° ì í\n", "\n", "for n in [len(newList)-1]:\n", " print(\"{0} ê³±ì \".format(n))\n", " for k in [(i) for i in range(1,9)]:\n", " print(\"{0} * {1} = {2}\".format(n,k,n*k))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### DICTIONARYì KEYS, VALUES,ITEMS" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A01\n", "A02\n", "A03\n", "KOR\n", "USA\n", "INDIA\n", "A01 KOR\n", "A02 USA\n", "A03 INDIA\n", "{'Kim': 1, 'Smith': 2, 'Hamish': 3, 'Singh': 4, 'Markus': 5}\n", "{'Kim': None, 'Smith': None, 'Hamish': None, 'Singh': None, 'Markus': None}\n" ] } ], "source": [ "# DICTIONARY í¨ìë¤ \n", "\n", "codeParam = {\"A01\":\"KOR\",\"A02\":\"USA\",\"A03\":\"INDIA\"}\n", "\n", "\n", "# for문ì¼ë¡ DICTIONARYì KEYê°ë§ ì¶ì¶\n", "for k in codeParam.keys():\n", " print(k)\n", "\n", "# for문ì¼ë¡ DICTIONARYì VALUEê°ë§ ì¶ì¶\n", "for v in codeParam.values():\n", " print(v)\n", "\n", "# for문ì¼ë¡ DICTIONARYì VALUEê°ë§ ì¶ì¶\n", "for k,v in codeParam.items():\n", " print(k,v)\n", " \n", "\n", "# DICTIONARY í¨ìë¤ 2 \n", "keyName = [\"Kim\",\"Smith\",\"Hamish\",\"Singh\",\"Markus\"]\n", "rank = [(i) for i in range(1,6)]\n", "\n", "# zipí¨ìë¡ ë 립ì ì¸ LIST ìë£ë¤ì í©ì¹¨ \n", "newDict1 = dict(zip(keyName,rank))\n", "print(newDict1)\n", "\n", "# 리ì¤í¸ìì ë°ì´í°ë¥¼ KEYê°ì¼ë¡ ê³ ì ìí¨í DICTIONARYë¡ ì í\n", "x= {key: value for key, value in dict.fromkeys(keyName).items() }\n", "print(x)\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.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }