Last active
February 11, 2017 06:15
-
-
Save wisaruthk/099adcfc5f6d17b084b208fe41719811 to your computer and use it in GitHub Desktop.
python collection example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Python Collection : tuple as key" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# ตั้งค่าข้อมูลตัวอย่าง \n", | |
"# ประเทศ, จังหวัด, เขต, จำนวนคน, จำนวนรถยนต์\n", | |
"raw_data = [\n", | |
" ['th','bkk','bangrak',300,220],\n", | |
" ['th','bkk','jatujak',400,230],\n", | |
" ['th','bkk','taksin',400,230]\n", | |
" ]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# เราจะทำการจัดกลุ่มข้อมูล โดยใช้ tuple เป็น key\n", | |
"group = {}\n", | |
"group_country = {}\n", | |
"for (country,province,zone,val1,val2) in raw_data:\n", | |
" group[(country,province,zone)] = val1\n", | |
" \n", | |
" # The method setdefault() is similar to get()\n", | |
" # ,but will set dict[key]=default if key is not already in dict.\n", | |
" # setdefault เหมือน get() ถ้า key ไม่มีจะ set ค่า default ให้\n", | |
" tot = group_country.setdefault(country,[0,0])\n", | |
" tot[0] += val1\n", | |
" tot[1] += val2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"collapsed": false, | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{('th', 'bkk', 'taksin'): 400, ('th', 'bkk', 'jatujak'): 400, ('th', 'bkk', 'bangrak'): 300}\n" | |
] | |
} | |
], | |
"source": [ | |
"# ข้อมูลกลุ่ม\n", | |
"print(group)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{'th': [1100, 680]}\n" | |
] | |
} | |
], | |
"source": [ | |
"# group by ชื่อประเทศ\n", | |
"print(group_country)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"400" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# ดึงข้อมูลโดยระบุ key เป็น tuple ประเทศ,จังหวัด,เขต\n", | |
"# สามารถเข้าถึงข้อมูลได้ง่าย\n", | |
"group[('th','bkk','taksin')]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{'th': [1100, 680]}\n" | |
] | |
} | |
], | |
"source": [ | |
"print(group_country)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1100, 680]" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# ดึงข้อมูลโดยระบุ key ด้วยชื่อประเทศ\n", | |
"group_country['th']" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"จังหวัด bkk เขต taksin ปริมาณคน 400\n", | |
"จังหวัด bkk เขต jatujak ปริมาณคน 400\n", | |
"จังหวัด bkk เขต bangrak ปริมาณคน 300\n" | |
] | |
} | |
], | |
"source": [ | |
"# แถมยังสามารถรับ params key ได้ตั้งแต่ตั้ง for loop เลยด้วย\n", | |
"for (country,province,state),val in group.items():\n", | |
" print('จังหวัด {} เขต {} ปริมาณคน {}'.format(province,state,val))" | |
] | |
} | |
], | |
"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.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment