Skip to content

Instantly share code, notes, and snippets.

@Chrislu30604
Created December 2, 2018 12:59
Show Gist options
  • Save Chrislu30604/5b7d035e9fb3f38d4a22b895aca354b1 to your computer and use it in GitHub Desktop.
Save Chrislu30604/5b7d035e9fb3f38d4a22b895aca354b1 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lambda Function"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Why Lambda ?\n",
"\n",
"- lambda 是個匿名函數 (anonymous function), 在程式碼上可以更優雅, 簡化繁瑣步驟\n",
"- can take any number of arguments, but can only have one expression\n",
"- 使用Function的主要目的為 :\n",
" 1. 降低重複的code\n",
" 2. 模組化\n",
" - 而當你不需要給予一個function名字時 (anonymous function), 則可以使用lambda"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Basic Usage"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### One argument"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2018-12-02T12:30:43.197917Z",
"start_time": "2018-12-02T12:30:43.194064Z"
},
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15\n"
]
}
],
"source": [
"x = lambda a : a + 10\n",
"print(x(5));"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Multiplies argument"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"13\n"
]
}
],
"source": [
" x = lambda a, b, c : a + b + c\n",
"print(x(5, 6, 2)) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Conditional statement"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"11\n"
]
}
],
"source": [
"f = lambda x: x if(x > 10) else x^2\n",
"print(f(9))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Lambda in Function"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"22\n"
]
}
],
"source": [
"def myfunc(n):\n",
" return lambda a : a * n\n",
"\n",
"mydoubler = myfunc(2)\n",
"\n",
"print(mydoubler(11))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Advanced Usage"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Combined with filter"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[5, 7, 97, 77, 23, 73, 61]\n"
]
}
],
"source": [
"li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61] \n",
"final_list = list(filter(lambda x: (x%2 != 0) , li)) \n",
"print(final_list) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Combined with map"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[10, 14, 44, 194, 108, 124, 154, 46, 146, 122]\n"
]
}
],
"source": [
"li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61] \n",
"final_list = list(map(lambda x: x*2 , li)) \n",
"print(final_list) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Combined with reduced "
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"193\n"
]
}
],
"source": [
"from functools import reduce\n",
"li = [5, 8, 10, 20, 50, 100] \n",
"sum = reduce((lambda x, y: x + y), li) \n",
"print (sum) #(5+8+10+20+50+100)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"hide_input": false,
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment