Commit 5e70280c authored by Loic Huder's avatar Loic Huder
Browse files

Added bonus question to the DIY on slicing

parent d591e589
...@@ -1051,7 +1051,8 @@ ...@@ -1051,7 +1051,8 @@
"- Extract the first field (e.g. using `find` method and slicing)\n", "- Extract the first field (e.g. using `find` method and slicing)\n",
"- Extract the last field (e.g. using `rfind` method and slicing)\n", "- Extract the last field (e.g. using `rfind` method and slicing)\n",
"- Check for empty field (e.g. `find` \";;\" pattern)\n", "- Check for empty field (e.g. `find` \";;\" pattern)\n",
"- Remove empty field (e.g. using replace method)" "- Remove empty field (e.g. using replace method)\n",
"- BONUS : Extract the second field (tip :`find` can take an argument that tells where to start the search)"
] ]
}, },
{ {
...@@ -1067,7 +1068,7 @@ ...@@ -1067,7 +1068,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 32, "execution_count": 12,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -1076,6 +1077,7 @@ ...@@ -1076,6 +1077,7 @@
"text": [ "text": [
"--wind;temperature;;pressure--\n", "--wind;temperature;;pressure--\n",
"wind\n", "wind\n",
"temperature\n",
"pressure\n", "pressure\n",
"16\n", "16\n",
"wind;temperature;pressure\n" "wind;temperature;pressure\n"
...@@ -1091,10 +1093,14 @@ ...@@ -1091,10 +1093,14 @@
"idx = s.find(\";\")\n", "idx = s.find(\";\")\n",
"s0 = s[0:idx]\n", "s0 = s[0:idx]\n",
"print(s0)\n", "print(s0)\n",
"# extract the last field\n", "# extract the second field\n",
"idx = s.rfind(\";\")\n", "idx1 = s.find(\";\", idx+1) # start the search after the first \";\"\n",
"s1 = s[idx+1:]\n", "s1 = s[idx+1:idx1]\n",
"print(s1)\n", "print(s1)\n",
"# extract the last field\n",
"idx2 = s.rfind(\";\")\n",
"s2 = s[idx2+1:]\n",
"print(s2)\n",
"idx_first_empty_field = s.find(\";;\")\n", "idx_first_empty_field = s.find(\";;\")\n",
"print(idx_first_empty_field)\n", "print(idx_first_empty_field)\n",
"# remove empty field\n", "# remove empty field\n",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment