Skip to content

Commit

Permalink
Created using Colaboratory
Browse files Browse the repository at this point in the history
  • Loading branch information
MachineLearnia committed Apr 25, 2020
1 parent e36a10b commit 8660767
Showing 1 changed file with 114 additions and 36 deletions.
150 changes: 114 additions & 36 deletions 05 - Dictionnaires.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"colab": {
"name": "Untitled2.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyORUSTiDA8mqmJXxXZVOgg0",
"authorship_tag": "ABX9TyNuaHloFlnajW67t9G5GL/M",
"include_colab_link": true
},
"kernelspec": {
Expand Down Expand Up @@ -59,16 +59,16 @@
"metadata": {
"id": "KIMrwYSF0WOx",
"colab_type": "code",
"outputId": "c44e65a3-4efa-4d94-e0a3-c64872120894",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "cee9251a-28a1-4013-86fd-87fc1d9bfe8b"
}
},
"source": [
"inventaire.values()"
],
"execution_count": 2,
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
Expand All @@ -80,7 +80,7 @@
"metadata": {
"tags": []
},
"execution_count": 2
"execution_count": 3
}
]
},
Expand All @@ -89,16 +89,16 @@
"metadata": {
"id": "kiVf5n2J0Yxa",
"colab_type": "code",
"outputId": "b829279b-d0cd-4df5-c4a1-57b1b5d1ea35",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "c23035ca-23be-4285-fee1-073cca94819c"
}
},
"source": [
"inventaire.keys()"
],
"execution_count": 3,
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
Expand All @@ -110,7 +110,7 @@
"metadata": {
"tags": []
},
"execution_count": 3
"execution_count": 4
}
]
},
Expand All @@ -119,28 +119,27 @@
"metadata": {
"id": "buwMNYZ40agi",
"colab_type": "code",
"outputId": "fef58cfb-e270-45f8-9d85-386d36ca86cf",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "94f1100e-2720-4145-f004-df3a9d14ccef"
"height": 163
}
},
"source": [
"len(inventaire)"
],
"execution_count": 4,
"execution_count": 1,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"3"
]
},
"metadata": {
"tags": []
},
"execution_count": 4
"output_type": "error",
"ename": "NameError",
"evalue": "ignored",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-1-afacfa14e4ab>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minventaire\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'inventaire' is not defined"
]
}
]
},
Expand All @@ -159,17 +158,17 @@
"metadata": {
"id": "0cPXXWkY0eCZ",
"colab_type": "code",
"outputId": "da888bd9-4c15-499c-c171-306d2dafb9eb",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "009e79bf-ade5-46d5-9713-cbba69d38bd9"
}
},
"source": [
"inventaire['abricots'] = 30\n",
"print(inventaire)"
],
"execution_count": 19,
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
Expand Down Expand Up @@ -208,16 +207,16 @@
"metadata": {
"id": "IlZRIOPb1Zd_",
"colab_type": "code",
"outputId": "28332b9a-6ae8-4196-e464-117f74f0b2d7",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "8d9183a9-48a1-4e04-895e-108694d02440"
}
},
"source": [
"inventaire.get('pommes') # pomme existe"
],
"execution_count": 15,
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
Expand All @@ -229,7 +228,7 @@
"metadata": {
"tags": []
},
"execution_count": 15
"execution_count": 7
}
]
},
Expand All @@ -248,18 +247,18 @@
"metadata": {
"id": "njAZZTkf1d7H",
"colab_type": "code",
"outputId": "b8f4a6a6-7e3f-4e3e-cb09-328ff4c24373",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
},
"outputId": "924c4ba2-1968-43ce-a7c8-ec52fc168048"
}
},
"source": [
"abricots = inventaire.pop(\"abricots\")\n",
"print(inventaire) # ne contient plus de clef abricots\n",
"print(abricots) # abricots contient la valeur du dictionnaire"
],
"execution_count": 20,
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
Expand All @@ -286,17 +285,17 @@
"metadata": {
"id": "pn0mt-wh1oj0",
"colab_type": "code",
"outputId": "69707cdc-8127-451e-c6ea-73942f0f1128",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 68
},
"outputId": "147023a8-577a-4464-b3a9-16f2426539e8"
}
},
"source": [
"for key, value in inventaire.items():\n",
" print(key, value)"
],
"execution_count": 21,
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
Expand All @@ -309,13 +308,92 @@
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qWrT3hUL3nbN",
"colab_type": "text"
},
"source": [
"## Exercice et Solution\n",
"Implémentez une fonction *trier(classeur, valeur)* qui place une valeur dans un dictionnaire en fonction de son signe"
]
},
{
"cell_type": "code",
"metadata": {
"id": "lvFl1n8Z22Pq",
"colab_type": "code",
"colab": {}
},
"source": [
"classeur = {'négatifs':[],\n",
" 'positifs':[]\n",
" }\n",
"\n",
"def trier(classeur, valeur):\n",
" return classeur"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "LAOsU1Ya4PKY",
"colab_type": "code",
"colab": {}
},
"source": [
"# SOLUTION\n",
"\n",
"def trier(classeur, valeur):\n",
" if valeur >=0:\n",
" classeur['positifs'].append(valeur)\n",
" else:\n",
" classeur['négatifs'].append(valeur)\n",
" return classeur"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "IvLbAkZB4rTk",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "7240704c-039f-4c65-daf0-187610f5a4f9"
},
"source": [
"trier(classeur, 9)"
],
"execution_count": 19,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'négatifs': [-2, -2, -4], 'positifs': [5, 9]}"
]
},
"metadata": {
"tags": []
},
"execution_count": 19
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "tpgKytJR4u0v",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
Expand Down

0 comments on commit 8660767

Please sign in to comment.