{
  "id": 284619,
  "title": "Python Functions",
  "url": "/competitions/sartorius-cell-instance-segmentation/discussion/284619",
  "author_name": "",
  "post_date": "2021-11-01T18:37:31.314464600Z",
  "votes": 2,
  "comment_count": 2,
  "views": 0,
  "content": "<p>I have seen in this notebook <a href=\"https://www.kaggle.com/remananr/image-rotation-using-opencv\" target=\"_blank\">https://www.kaggle.com/remananr/image-rotation-using-opencv</a> the following:</p>\n<blockquote>\n  <p>s = mask_rle.split()<br>\n    starts = list(map(lambda x: int(x) - 1, s[0::2]))</p>\n</blockquote>\n<p>and I don't comprehend what is s[0::2] in this context, and also what map(lambda x: int(x) - 1, s[0::2])) does. Could anyone clear it out?</p>",
  "messages": [
    {
      "id": "1567297",
      "postDate": "11/01/2021 18:37:31",
      "content": "<p>I have seen in this notebook <a href=\"https://www.kaggle.com/remananr/image-rotation-using-opencv\" target=\"_blank\">https://www.kaggle.com/remananr/image-rotation-using-opencv</a> the following:</p>\n<blockquote>\n  <p>s = mask_rle.split()<br>\n    starts = list(map(lambda x: int(x) - 1, s[0::2]))</p>\n</blockquote>\n<p>and I don't comprehend what is s[0::2] in this context, and also what map(lambda x: int(x) - 1, s[0::2])) does. Could anyone clear it out?</p>",
      "rawMarkdown": "I have seen in this notebook https://www.kaggle.com/remananr/image-rotation-using-opencv the following:\n\n> s = mask_rle.split()\n  starts = list(map(lambda x: int(x) - 1, s[0::2]))\n\nand I don't comprehend what is s[0::2] in this context, and also what map(lambda x: int(x) - 1, s[0::2])) does. Could anyone clear it out?",
      "votes": null
    },
    {
      "id": "1569444",
      "postDate": "11/03/2021 12:51:49",
      "content": "<p><code>s[0::2]</code> is simply an instance of slicing. In this case, <code>s[0::2]</code> will pick all even indices of the array s. To understand this in detail, have a look at <a href=\"https://stackoverflow.com/a/509295\" target=\"_blank\">this answer on StackOverflow</a>.</p>\n<p>map is an <a href=\"https://docs.python.org/3/library/functions.html#map\" target=\"_blank\">inbuilt python function</a> that takes any function and an iterable(in this case, a list) as its inputs. It applies the passed function to every element of the passed iterable.<br>\nFor example, <code>list(map(float, [1, 2, 3]))</code> will first map all elements of the passed array to float and then typecast it to a list. The output will be <code>[1.0, 2.0, 3.0]</code>.</p>\n<p>Finally, <a href=\"https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions\" target=\"_blank\">lambda functions</a> can be used to make functions in one line. The syntax is<code>lambda inputs:outputs</code>. </p>\n<p>TLDR;<br>\n<code>lambda x: int(x) - 1</code> is a function that takes x as an input, converts it to an integer, subtracts 1 from it and returns the result.</p>\n<p><strong><code>starts = list(map(lambda x: int(x) - 1, s[0::2]))</code> returns a list of integers formed by subtracting 1 from the elements on even indices of array s and assigns it to <code>starts</code>.</strong></p>",
      "rawMarkdown": "`s[0::2]` is simply an instance of slicing. In this case, `s[0::2]` will pick all even indices of the array s. To understand this in detail, have a look at [this answer on StackOverflow](https://stackoverflow.com/a/509295).\n\nmap is an [inbuilt python function](https://docs.python.org/3/library/functions.html#map) that takes any function and an iterable(in this case, a list) as its inputs. It applies the passed function to every element of the passed iterable.\nFor example, `list(map(float, [1, 2, 3]))` will first map all elements of the passed array to float and then typecast it to a list. The output will be `[1.0, 2.0, 3.0]`.\n\nFinally, [lambda functions](https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions) can be used to make functions in one line. The syntax is` lambda inputs:outputs`. \n\nTLDR;\n`lambda x: int(x) - 1` is a function that takes x as an input, converts it to an integer, subtracts 1 from it and returns the result.\n\n**`starts = list(map(lambda x: int(x) - 1, s[0::2]))` returns a list of integers formed by subtracting 1 from the elements on even indices of array s and assigns it to `starts`.**",
      "votes": null
    },
    {
      "id": "1570205",
      "postDate": "11/04/2021 01:28:51",
      "content": "<p>What is the map that we have in map(lambda x: int(x) - 1, s[0::2])?</p>",
      "rawMarkdown": "What is the map that we have in map(lambda x: int(x) - 1, s[0::2])?",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1569444,
      "author_name": "ther3venant",
      "author_url": "",
      "post_date": "11/03/2021 12:51:49",
      "content": "<p><code>s[0::2]</code> is simply an instance of slicing. In this case, <code>s[0::2]</code> will pick all even indices of the array s. To understand this in detail, have a look at <a href=\"https://stackoverflow.com/a/509295\" target=\"_blank\">this answer on StackOverflow</a>.</p>\n<p>map is an <a href=\"https://docs.python.org/3/library/functions.html#map\" target=\"_blank\">inbuilt python function</a> that takes any function and an iterable(in this case, a list) as its inputs. It applies the passed function to every element of the passed iterable.<br>\nFor example, <code>list(map(float, [1, 2, 3]))</code> will first map all elements of the passed array to float and then typecast it to a list. The output will be <code>[1.0, 2.0, 3.0]</code>.</p>\n<p>Finally, <a href=\"https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions\" target=\"_blank\">lambda functions</a> can be used to make functions in one line. The syntax is<code>lambda inputs:outputs</code>. </p>\n<p>TLDR;<br>\n<code>lambda x: int(x) - 1</code> is a function that takes x as an input, converts it to an integer, subtracts 1 from it and returns the result.</p>\n<p><strong><code>starts = list(map(lambda x: int(x) - 1, s[0::2]))</code> returns a list of integers formed by subtracting 1 from the elements on even indices of array s and assigns it to <code>starts</code>.</strong></p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 1570205,
      "author_name": "joaovictormelo",
      "author_url": "",
      "post_date": "11/04/2021 01:28:51",
      "content": "<p>What is the map that we have in map(lambda x: int(x) - 1, s[0::2])?</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1567297": "I have seen in this notebook https://www.kaggle.com/remananr/image-rotation-using-opencv the following:\n\n> s = mask_rle.split()\n  starts = list(map(lambda x: int(x) - 1, s[0::2]))\n\nand I don't comprehend what is s[0::2] in this context, and also what map(lambda x: int(x) - 1, s[0::2])) does. Could anyone clear it out?",
    "1569444": "`s[0::2]` is simply an instance of slicing. In this case, `s[0::2]` will pick all even indices of the array s. To understand this in detail, have a look at [this answer on StackOverflow](https://stackoverflow.com/a/509295).\n\nmap is an [inbuilt python function](https://docs.python.org/3/library/functions.html#map) that takes any function and an iterable(in this case, a list) as its inputs. It applies the passed function to every element of the passed iterable.\nFor example, `list(map(float, [1, 2, 3]))` will first map all elements of the passed array to float and then typecast it to a list. The output will be `[1.0, 2.0, 3.0]`.\n\nFinally, [lambda functions](https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions) can be used to make functions in one line. The syntax is` lambda inputs:outputs`. \n\nTLDR;\n`lambda x: int(x) - 1` is a function that takes x as an input, converts it to an integer, subtracts 1 from it and returns the result.\n\n**`starts = list(map(lambda x: int(x) - 1, s[0::2]))` returns a list of integers formed by subtracting 1 from the elements on even indices of array s and assigns it to `starts`.**",
    "1570205": "What is the map that we have in map(lambda x: int(x) - 1, s[0::2])?"
  },
  "source": "meta"
}