{"id":168,"date":"2023-12-19T09:31:43","date_gmt":"2023-12-19T15:31:43","guid":{"rendered":"https:\/\/www.baizhao666.com\/?p=168"},"modified":"2024-07-17T18:47:04","modified_gmt":"2024-07-18T00:47:04","slug":"lambda-map-filter-and-arbitrary-inputs","status":"publish","type":"post","link":"https:\/\/www.baizhao666.com\/?p=168","title":{"rendered":"Lambda, map, filter, and arbitrary inputs"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Lambda functions<\/h3>\n\n\n\n<p>The Lambda functions are nice for Calculus functions or ant one-line function with a simple input-output format. They can also be used to create functions &#8220;anonymously&#8221;. A Lambda function can take any number of arguments, but can only have one expression. Using a Lambda function, we can make our codes more concise. <\/p>\n\n\n\n<!--more-->\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># The syntax for Lambda is:\nlambda&lt;argument(s)&gt;: &lt;expression&gt;\n\n# suppose a normal function: f(x)=x^3\n# function in Python\ndef f(x):\n    return x**3\n# Lambda\nf = lambda x: x**3\n\n# another normal function: g(x,y,z)=1\/2*x*y*z\n# Lambda\ng = lambda x, y, z: 1\/2 * x * y * z<\/code><\/pre><\/div>\n\n\n\n<p>Lambda functions can be used without naming them. For instance, some functions require a function as input, and a lambda can be directly plugged in as the input. The power of lambda is also shown when you use them as an anonymous function inside another function. <\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># Lambda inside other function\ndef my_fun(n):\n    return lambda x: x ** n\n# using my_fun\nfoo = my_fun(3)\nprint(foo(5))  \n# the result is 5**3 = 125<\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Map<\/h3>\n\n\n\n<p>Another use of Lambda is in the &#8220;map&#8221; function. The &#8220;map&#8221; function in python will &#8220;map&#8221; all the values of a iterable container (mostly a list) using a given function. As in, all the values will be plugged into the function and transformed accordingly. These functions used in the &#8220;map&#8221; function are mostly Lambda functions.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># the syntax of map:\nmap(function, iterables)\n# it will return a map object, we can use list() to turn it into a list\n\n# suppose there is a list L, generate another list using the values in L but square them\n&gt;&gt;&gt; L = [3, 5, 1, 9]\n&gt;&gt;&gt; m = map(lambda x: x**2, L)\n&gt;&gt;&gt; print(list(m))\n[9, 25, 1, 81]\n\n# map function also works for multiple iterable objects\n&gt;&gt;&gt; L1 = [1, 3, 5, 7]\n&gt;&gt;&gt; L2 = [2, 4, 6, 8]\n&gt;&gt;&gt; m = map(lambda a, b: a + b, L1, L2)\n&gt;&gt;&gt; print(list(m))\n[3, 7, 11, 15]\n# here I have two lists, and a lambda function that used in map function to add the according numbers in these two lists<\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Filter<\/h3>\n\n\n\n<p>Filter is used to pick out the elements of a iterable container (mostly a list) that satisfy some conditions. These conditions are mostly given by some Lambda functions. Here the Lambda function needs to be an expression that return either true or false.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># the syntax of filter:\nfilter(function, iterable)\n# it will return a filter object, we can use list() to turn it into a list\n\n# suppose there is a list L, generate a new list using the values in L but need to be greater than 5\n&gt;&gt;&gt; L = [8, 1, 6, 2, 7]\n&gt;&gt;&gt; f = filter(lambda x: x &gt; 5, L)\n&gt;&gt;&gt; print(list(f))\n[8, 6, 7]\n# using filter, we can get the values in L which are greater than 5\n# here filter function can only work for 1 iterable object\n\n# we can also combine filter and map\n# nested vision:\n&gt;&gt;&gt; L = [8, 1, 6, 2, 7]\n&gt;&gt;&gt; c = map(lambda x: x ** 2, filter(lambda x: x &gt;= 5, L))\n&gt;&gt;&gt; print(list(c))\n[64, 36, 49]\n# here the filter function will be executed first, which picks the values that are &gt;= 5\n# then the map function will square those values<\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Arbitrary inputs<\/h3>\n\n\n\n<p>Sometimes in the main function of C, we can see int main(int argc, char* argv[]). Here argc stands for argument count, and argv stands for argument values. which will be used for arbitrary inputs. In python, there is a similar keyword for arbitrary input arguments called &#8220;*args&#8221; which stands for &#8220;arguments&#8221;. We can use &#8220;*args&#8221; to allow a function in Python to have arbitrarily many inputs, especially in the case when we don&#8217;t know how many inputs will be passed into our function. Although &#8220;*args&#8221; stands for arguments, it can be any names, not necessarily be &#8220;args&#8221;. But a &#8220;*&#8221; needs to be added before the argument&#8217;s name. On the other hand, we should treat &#8220;*args&#8221; as a tuple. In this case, the arguments in &#8220;*args&#8221; can be 0 or as many as we want.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># example of arbitrary arguments\n&gt;&gt;&gt; def print_all(*args):\n...     print(type(args))\n...     print(&quot;Here are the content of *argus:&quot;)\n...     for val in args:\n...             print(val)\n...\n&gt;&gt;&gt; print_all(1, &quot;apple&quot;, True, 3.14)\n&lt;class &#39;tuple&#39;&gt;   # the data type of &quot;*argus&quot;\nHere are the content of *argus:\n1\napple\nTrue\n3.14\n\n# we can have regular inputs AND arbitrary input, but be sure to put &quot;*args&quot; at last\n# for instance:\ndef foo(var_1, var_2, *args)\n# we can also give a default value to our input argument\ndef foo(x = 5)\n# in this case, if there is no input, python will assume x is 5\n\n# small note: if we want to pass all the values of &quot;*args&quot; to another function\n# be sure to include &quot;*&quot; when passing, otherwise, it will be a tuple in tuple\n&gt;&gt;&gt; def foo1(*args):\n...     for val in args:\n...             print(val)\n...\n&gt;&gt;&gt; def foo2(*args):\n...     foo1(*args)\n...\n&gt;&gt;&gt; foo2(1, &quot;apple&quot;, True, 3.14)\n1\napple\nTrue\n3.14<\/code><\/pre><\/div>\n\n\n\n<p>There is another arbitrary input arguments called &#8220;**kwargs&#8221;. &#8220;**kwargs&#8221; stands for keyword arguments. We should treat kwargs as a <strong>DICTIONARY<\/strong>. When passing in arguments, we should use the syntax of key=value.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># example of **kwargs\ndef foo1(**kwargs):\n    for k in kwargs:\n        print(f&quot;key={k}, value={kwargs[k]}&quot;)\n# pass in arguments\nfoo1(fruit=&quot;apple&quot;, value_of_pi=3.14)\n# result:\nkey=fruit, value=apple\nkey=value_of_pi, value=3.14\n\n# similar to &quot;*args&quot;, when passing all the values in &quot;**kwargs&quot; to another function\n# be sure the &quot;**&quot; was added before the kwargs\ndef foo1(**kwargs):\n    for k in kwargs:\n        print(f&quot;key={k}, value={kwargs[k]}&quot;)\n\ndef foo2(**kwargs):\n    foo1(**kwargs)\n<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Lambda functions The Lambda functions are nice for Calculus functions or ant one-line function with a simple input-output format. They can also be used to create functions &#8220;anonymously&#8221;. A Lambda function can take any number of arguments, but can only have one expression. Using a Lambda function, we can make our codes more concise.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-168","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=\/wp\/v2\/posts\/168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=168"}],"version-history":[{"count":7,"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=\/wp\/v2\/posts\/168\/revisions"}],"predecessor-version":[{"id":280,"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=\/wp\/v2\/posts\/168\/revisions\/280"}],"wp:attachment":[{"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}