diff --git a/lab-list-comprehensions/your-code/main.ipynb b/lab-list-comprehensions/your-code/main.ipynb index 9860215..aa04155 100644 --- a/lab-list-comprehensions/your-code/main.ipynb +++ b/lab-list-comprehensions/your-code/main.ipynb @@ -11,7 +11,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -29,10 +29,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]\n" + ] + } + ], + "source": [ + "# 1. Create a list of numbers from 1 to 50\n", + "list1 = [i for i in range(1,51)]\n", + "print(list1)" + ] }, { "cell_type": "markdown", @@ -43,10 +55,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200]\n" + ] + } + ], + "source": [ + "# 2. Create a list of even numbers from 2 to 200\n", + "list2 = [i for i in range(2,201) if i%2==0]\n", + "print(list2)" + ] }, { "cell_type": "markdown", @@ -57,10 +81,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[array([0.84062117, 0.48006452, 0.7876326 , 0.77109654]), array([0.44409793, 0.09014516, 0.81835917, 0.87645456]), array([0.7066597 , 0.09610873, 0.41247947, 0.57433389]), array([0.29960807, 0.42315023, 0.34452557, 0.4751035 ]), array([0.17003563, 0.46843998, 0.92796258, 0.69814654]), array([0.41290051, 0.19561071, 0.16284783, 0.97016248]), array([0.71725408, 0.87702738, 0.31244595, 0.76615487]), array([0.20754036, 0.57871812, 0.07214068, 0.40356048]), array([0.12149553, 0.53222417, 0.9976855 , 0.12536346]), array([0.80930099, 0.50962849, 0.94555126, 0.33364763])]\n" + ] + }, + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ + "# 3. Create a list containing all the elements of array1\n", "a = np.array([[0.84062117, 0.48006452, 0.7876326 , 0.77109654],\n", " [0.44409793, 0.09014516, 0.81835917, 0.87645456],\n", " [0.7066597 , 0.09610873, 0.41247947, 0.57433389],\n", @@ -70,15 +113,53 @@ " [0.71725408, 0.87702738, 0.31244595, 0.76615487],\n", " [0.20754036, 0.57871812, 0.07214068, 0.40356048],\n", " [0.12149553, 0.53222417, 0.9976855 , 0.12536346],\n", - " [0.80930099, 0.50962849, 0.94555126, 0.33364763]])" + " [0.80930099, 0.50962849, 0.94555126, 0.33364763]])\n", + "\n", + "list3 = [i for i in a]\n", + "print(list3)\n", + "type(list3)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.84062117\n", + "0.7876326\n", + "0.77109654\n", + "0.81835917\n", + "0.87645456\n", + "0.7066597\n", + "0.57433389\n", + "0.92796258\n", + "0.69814654\n", + "0.97016248\n", + "0.71725408\n", + "0.87702738\n", + "0.76615487\n", + "0.57871812\n", + "0.53222417\n", + "0.9976855\n", + "0.80930099\n", + "0.50962849\n", + "0.94555126\n" + ] + } + ], + "source": [ + "# 4. Extract elements from list3 that are greater or equal than 0.5\n", + "def extract():\n", + " for i in list3:\n", + " for j in i:\n", + " if j >= 0.5:\n", + " print(j)\n", + "extract()" + ] }, { "cell_type": "markdown", @@ -89,10 +170,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[array([0.84062117, 0.48006452, 0.7876326 , 0.77109654]), array([0.44409793, 0.09014516, 0.81835917, 0.87645456]), array([0.7066597 , 0.09610873, 0.41247947, 0.57433389]), array([0.29960807, 0.42315023, 0.34452557, 0.4751035 ]), array([0.17003563, 0.46843998, 0.92796258, 0.69814654]), array([0.41290051, 0.19561071, 0.16284783, 0.97016248]), array([0.71725408, 0.87702738, 0.31244595, 0.76615487]), array([0.20754036, 0.57871812, 0.07214068, 0.40356048]), array([0.12149553, 0.53222417, 0.9976855 , 0.12536346]), array([0.80930099, 0.50962849, 0.94555126, 0.33364763])]\n" + ] + }, + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 4. Add a condition to the previous list comprehension to only include elements that are greater or equal to 0.5\n", + "list4 = [i for i in list3 if i.all() >= 0.5]\n", + "print(list4)\n", + "type (list4)" + ] }, { "cell_type": "markdown", @@ -103,7 +207,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 84, "metadata": {}, "outputs": [], "source": [ @@ -125,10 +229,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 85, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.55867166, 0.06210792, 0.08147297, 0.82579068, 0.91512478, 0.06833034, 0.05440634, 0.65857693, 0.30296619, 0.06769833, 0.96031863, 0.51293743, 0.09143215, 0.71893382, 0.45850679, 0.58256464, 0.59005654, 0.56266457, 0.71600294, 0.87392666, 0.11434044, 0.8694668, 0.65669313, 0.10708681, 0.07529684, 0.46470767, 0.47984544, 0.65368638, 0.14901286, 0.23760688]\n" + ] + }, + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 85, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 5. Create a list of all the elements of the 5x3x2 array b\n", + "list5 = [z for x in b for y in x for z in y]\n", + "print(list5)\n", + "type(list5)" + ] }, { "cell_type": "markdown", @@ -139,10 +266,52 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 130, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.08147297\n", + "0.06833034\n", + "0.30296619\n", + "Nope\n", + "0.45850679\n", + "Nope\n", + "0.11434044\n", + "0.10708681\n", + "0.47984544\n", + "0.23760688\n" + ] + } + ], + "source": [ + "for y in b:\n", + " for z in y:\n", + " if z[-1] <= 0.5:\n", + " print(z[-1])\n", + " else: print('Nope')\n" + ] + }, + { + "cell_type": "code", + "execution_count": 113, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.08147297, 0.06833034, 0.30296619, 0.45850679, 0.11434044, 0.10708681, 0.47984544, 0.23760688]\n" + ] + } + ], + "source": [ + "# print last value of sublists of list5\n", + "list6 = [z[-1] for y in b for z in y if z[-1] <= 0.5]\n", + "print(list6)\n" + ] }, { "cell_type": "markdown", @@ -153,10 +322,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 67, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['sample_file_0.csv', 'sample_file_1.csv', 'sample_file_2.csv', 'sample_file_3.csv', 'sample_file_4.csv', 'sample_file_5.csv', 'sample_file_6.csv', 'sample_file_7.csv', 'sample_file_8.csv', 'sample_file_9.csv']\n" + ] + } + ], + "source": [ + "# 6. Select and print the names of all CSV files in the data folder\n", + "file_list = [i for i in os.listdir('C:/Users/TEMP.GDLCMS-PC/Documents/GitHub/Lab-list-comprehension/lab-list-comprehensions/data') if i.endswith('.csv')]\n", + "print(file_list)" + ] }, { "cell_type": "markdown", @@ -167,10 +348,335 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 133, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + " | 0 | \n", + "1 | \n", + "2 | \n", + "3 | \n", + "4 | \n", + "5 | \n", + "6 | \n", + "7 | \n", + "8 | \n", + "9 | \n", + "10 | \n", + "11 | \n", + "12 | \n", + "13 | \n", + "14 | \n", + "15 | \n", + "16 | \n", + "17 | \n", + "18 | \n", + "19 | \n", + "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", + "0.734751 | \n", + "0.195362 | \n", + "0.734309 | \n", + "0.598184 | \n", + "0.763433 | \n", + "0.263434 | \n", + "0.868066 | \n", + "0.058092 | \n", + "0.753502 | \n", + "0.587513 | \n", + "0.311608 | \n", + "0.178356 | \n", + "0.182922 | \n", + "0.147631 | \n", + "0.391188 | \n", + "0.816049 | \n", + "0.749068 | \n", + "0.293260 | \n", + "0.937828 | \n", + "0.880858 | \n", + "
1 | \n", + "0.772607 | \n", + "0.445391 | \n", + "0.249642 | \n", + "0.787922 | \n", + "0.598583 | \n", + "0.827238 | \n", + "0.624126 | \n", + "0.601524 | \n", + "0.688753 | \n", + "0.338870 | \n", + "0.081595 | \n", + "0.471474 | \n", + "0.267443 | \n", + "0.453351 | \n", + "0.800716 | \n", + "0.045749 | \n", + "0.683793 | \n", + "0.389789 | \n", + "0.016787 | \n", + "0.503695 | \n", + "
2 | \n", + "0.226428 | \n", + "0.268764 | \n", + "0.694262 | \n", + "0.622335 | \n", + "0.063843 | \n", + "0.122683 | \n", + "0.815625 | \n", + "0.584542 | \n", + "0.032594 | \n", + "0.589775 | \n", + "0.764350 | \n", + "0.650973 | \n", + "0.565705 | \n", + "0.691784 | \n", + "0.265223 | \n", + "0.739031 | \n", + "0.560394 | \n", + "0.334802 | \n", + "0.517694 | \n", + "0.646110 | \n", + "
3 | \n", + "0.362748 | \n", + "0.495430 | \n", + "0.113876 | \n", + "0.594149 | \n", + "0.612522 | \n", + "0.625204 | \n", + "0.864050 | \n", + "0.260279 | \n", + "0.528873 | \n", + "0.168043 | \n", + "0.715929 | \n", + "0.677014 | \n", + "0.175735 | \n", + "0.632370 | \n", + "0.926715 | \n", + "0.085675 | \n", + "0.120525 | \n", + "0.141746 | \n", + "0.771144 | \n", + "0.489660 | \n", + "
4 | \n", + "0.033415 | \n", + "0.340433 | \n", + "0.464971 | \n", + "0.363737 | \n", + "0.025815 | \n", + "0.434129 | \n", + "0.415163 | \n", + "0.892210 | \n", + "0.381701 | \n", + "0.415264 | \n", + "0.790801 | \n", + "0.696930 | \n", + "0.819751 | \n", + "0.944029 | \n", + "0.869965 | \n", + "0.041723 | \n", + "0.819140 | \n", + "0.676051 | \n", + "0.109349 | \n", + "0.872947 | \n", + "
0 | \n", + "0.276827 | \n", + "0.260054 | \n", + "0.942397 | \n", + "0.113187 | \n", + "0.781355 | \n", + "0.475740 | \n", + "0.152061 | \n", + "0.250324 | \n", + "0.147078 | \n", + "0.162984 | \n", + "0.977025 | \n", + "0.509619 | \n", + "0.593212 | \n", + "0.911839 | \n", + "0.257645 | \n", + "0.386457 | \n", + "0.696932 | \n", + "0.069162 | \n", + "0.952291 | \n", + "0.286542 | \n", + "
1 | \n", + "0.995885 | \n", + "0.158381 | \n", + "0.244274 | \n", + "0.962163 | \n", + "0.651900 | \n", + "0.930665 | \n", + "0.577190 | \n", + "0.087914 | \n", + "0.960261 | \n", + "0.580840 | \n", + "0.194616 | \n", + "0.661459 | \n", + "0.674085 | \n", + "0.049326 | \n", + "0.785803 | \n", + "0.315645 | \n", + "0.495355 | \n", + "0.232135 | \n", + "0.549324 | \n", + "0.572232 | \n", + "
2 | \n", + "0.641917 | \n", + "0.821055 | \n", + "0.392437 | \n", + "0.782617 | \n", + "0.510762 | \n", + "0.428320 | \n", + "0.017324 | \n", + "0.680720 | \n", + "0.340412 | \n", + "0.462513 | \n", + "0.785776 | \n", + "0.251949 | \n", + "0.032847 | \n", + "0.995700 | \n", + "0.816563 | \n", + "0.735692 | \n", + "0.435998 | \n", + "0.430411 | \n", + "0.531757 | \n", + "0.489528 | \n", + "
3 | \n", + "0.806532 | \n", + "0.569258 | \n", + "0.148175 | \n", + "0.809987 | \n", + "0.459632 | \n", + "0.735762 | \n", + "0.730664 | \n", + "0.934502 | \n", + "0.080322 | \n", + "0.763502 | \n", + "0.398504 | \n", + "0.027637 | \n", + "0.409665 | \n", + "0.942846 | \n", + "0.133256 | \n", + "0.157158 | \n", + "0.929446 | \n", + "0.402791 | \n", + "0.685976 | \n", + "0.246594 | \n", + "
4 | \n", + "0.311185 | \n", + "0.501165 | \n", + "0.365979 | \n", + "0.782807 | \n", + "0.776795 | \n", + "0.797199 | \n", + "0.791946 | \n", + "0.847157 | \n", + "0.771811 | \n", + "0.233944 | \n", + "0.522344 | \n", + "0.053030 | \n", + "0.208551 | \n", + "0.824354 | \n", + "0.588567 | \n", + "0.604341 | \n", + "0.232964 | \n", + "0.229109 | \n", + "0.022881 | \n", + "0.479022 | \n", + "
\n", + " | 0 | \n", + "1 | \n", + "2 | \n", + "3 | \n", + "4 | \n", + "5 | \n", + "6 | \n", + "7 | \n", + "8 | \n", + "9 | \n", + "... | \n", + "11 | \n", + "12 | \n", + "13 | \n", + "14 | \n", + "15 | \n", + "16 | \n", + "17 | \n", + "18 | \n", + "19 | \n", + "20 | \n", + "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", + "0.734751 | \n", + "0.195362 | \n", + "0.734309 | \n", + "0.598184 | \n", + "0.763433 | \n", + "0.263434 | \n", + "0.868066 | \n", + "0.058092 | \n", + "0.753502 | \n", + "0.587513 | \n", + "... | \n", + "0.178356 | \n", + "0.182922 | \n", + "0.147631 | \n", + "0.391188 | \n", + "0.816049 | \n", + "0.749068 | \n", + "0.293260 | \n", + "0.937828 | \n", + "0.880858 | \n", + "0.780858 | \n", + "
1 | \n", + "0.772607 | \n", + "0.445391 | \n", + "0.249642 | \n", + "0.787922 | \n", + "0.598583 | \n", + "0.827238 | \n", + "0.624126 | \n", + "0.601524 | \n", + "0.688753 | \n", + "0.338870 | \n", + "... | \n", + "0.471474 | \n", + "0.267443 | \n", + "0.453351 | \n", + "0.800716 | \n", + "0.045749 | \n", + "0.683793 | \n", + "0.389789 | \n", + "0.016787 | \n", + "0.503695 | \n", + "0.403695 | \n", + "
2 | \n", + "0.226428 | \n", + "0.268764 | \n", + "0.694262 | \n", + "0.622335 | \n", + "0.063843 | \n", + "0.122683 | \n", + "0.815625 | \n", + "0.584542 | \n", + "0.032594 | \n", + "0.589775 | \n", + "... | \n", + "0.650973 | \n", + "0.565705 | \n", + "0.691784 | \n", + "0.265223 | \n", + "0.739031 | \n", + "0.560394 | \n", + "0.334802 | \n", + "0.517694 | \n", + "0.646110 | \n", + "0.546110 | \n", + "
3 | \n", + "0.362748 | \n", + "0.495430 | \n", + "0.113876 | \n", + "0.594149 | \n", + "0.612522 | \n", + "0.625204 | \n", + "0.864050 | \n", + "0.260279 | \n", + "0.528873 | \n", + "0.168043 | \n", + "... | \n", + "0.677014 | \n", + "0.175735 | \n", + "0.632370 | \n", + "0.926715 | \n", + "0.085675 | \n", + "0.120525 | \n", + "0.141746 | \n", + "0.771144 | \n", + "0.489660 | \n", + "0.389660 | \n", + "
4 | \n", + "0.033415 | \n", + "0.340433 | \n", + "0.464971 | \n", + "0.363737 | \n", + "0.025815 | \n", + "0.434129 | \n", + "0.415163 | \n", + "0.892210 | \n", + "0.381701 | \n", + "0.415264 | \n", + "... | \n", + "0.696930 | \n", + "0.819751 | \n", + "0.944029 | \n", + "0.869965 | \n", + "0.041723 | \n", + "0.819140 | \n", + "0.676051 | \n", + "0.109349 | \n", + "0.872947 | \n", + "0.772947 | \n", + "
0 | \n", + "0.276827 | \n", + "0.260054 | \n", + "0.942397 | \n", + "0.113187 | \n", + "0.781355 | \n", + "0.475740 | \n", + "0.152061 | \n", + "0.250324 | \n", + "0.147078 | \n", + "0.162984 | \n", + "... | \n", + "0.509619 | \n", + "0.593212 | \n", + "0.911839 | \n", + "0.257645 | \n", + "0.386457 | \n", + "0.696932 | \n", + "0.069162 | \n", + "0.952291 | \n", + "0.286542 | \n", + "0.186542 | \n", + "
1 | \n", + "0.995885 | \n", + "0.158381 | \n", + "0.244274 | \n", + "0.962163 | \n", + "0.651900 | \n", + "0.930665 | \n", + "0.577190 | \n", + "0.087914 | \n", + "0.960261 | \n", + "0.580840 | \n", + "... | \n", + "0.661459 | \n", + "0.674085 | \n", + "0.049326 | \n", + "0.785803 | \n", + "0.315645 | \n", + "0.495355 | \n", + "0.232135 | \n", + "0.549324 | \n", + "0.572232 | \n", + "0.472232 | \n", + "
2 | \n", + "0.641917 | \n", + "0.821055 | \n", + "0.392437 | \n", + "0.782617 | \n", + "0.510762 | \n", + "0.428320 | \n", + "0.017324 | \n", + "0.680720 | \n", + "0.340412 | \n", + "0.462513 | \n", + "... | \n", + "0.251949 | \n", + "0.032847 | \n", + "0.995700 | \n", + "0.816563 | \n", + "0.735692 | \n", + "0.435998 | \n", + "0.430411 | \n", + "0.531757 | \n", + "0.489528 | \n", + "0.389528 | \n", + "
3 | \n", + "0.806532 | \n", + "0.569258 | \n", + "0.148175 | \n", + "0.809987 | \n", + "0.459632 | \n", + "0.735762 | \n", + "0.730664 | \n", + "0.934502 | \n", + "0.080322 | \n", + "0.763502 | \n", + "... | \n", + "0.027637 | \n", + "0.409665 | \n", + "0.942846 | \n", + "0.133256 | \n", + "0.157158 | \n", + "0.929446 | \n", + "0.402791 | \n", + "0.685976 | \n", + "0.246594 | \n", + "0.146594 | \n", + "
4 | \n", + "0.311185 | \n", + "0.501165 | \n", + "0.365979 | \n", + "0.782807 | \n", + "0.776795 | \n", + "0.797199 | \n", + "0.791946 | \n", + "0.847157 | \n", + "0.771811 | \n", + "0.233944 | \n", + "... | \n", + "0.053030 | \n", + "0.208551 | \n", + "0.824354 | \n", + "0.588567 | \n", + "0.604341 | \n", + "0.232964 | \n", + "0.229109 | \n", + "0.022881 | \n", + "0.479022 | \n", + "0.379022 | \n", + "
10 rows × 21 columns
\n", + "