From 05f5a961ec395260f8061bfd23fe9fd67381904a Mon Sep 17 00:00:00 2001 From: Montserrat Labrada Date: Sun, 9 Oct 2022 19:55:51 -0400 Subject: [PATCH] completed lab list comprehension --- lab-list-comprehensions/your-code/main.ipynb | 179 ++++++++++++++++--- 1 file changed, 152 insertions(+), 27 deletions(-) diff --git a/lab-list-comprehensions/your-code/main.ipynb b/lab-list-comprehensions/your-code/main.ipynb index 9860215..9958aa8 100644 --- a/lab-list-comprehensions/your-code/main.ipynb +++ b/lab-list-comprehensions/your-code/main.ipynb @@ -29,10 +29,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "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": [ + "list=[i for i in range (1,51)]\n", + "print(list)" + ] }, { "cell_type": "markdown", @@ -43,10 +54,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "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": [ + "list_even=[i for i in range (2, 201) if i%2==0]\n", + "print(list_even)" + ] }, { "cell_type": "markdown", @@ -75,10 +97,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0.84062117, 0.48006452, 0.7876326, 0.77109654], [0.44409793, 0.09014516, 0.81835917, 0.87645456], [0.7066597, 0.09610873, 0.41247947, 0.57433389], [0.29960807, 0.42315023, 0.34452557, 0.4751035], [0.17003563, 0.46843998, 0.92796258, 0.69814654], [0.41290051, 0.19561071, 0.16284783, 0.97016248], [0.71725408, 0.87702738, 0.31244595, 0.76615487], [0.20754036, 0.57871812, 0.07214068, 0.40356048], [0.12149553, 0.53222417, 0.9976855, 0.12536346], [0.80930099, 0.50962849, 0.94555126, 0.33364763]]\n", + "[0.84062117, 0.48006452, 0.7876326, 0.77109654, 0.44409793, 0.09014516, 0.81835917, 0.87645456, 0.7066597, 0.09610873, 0.41247947, 0.57433389, 0.29960807, 0.42315023, 0.34452557, 0.4751035, 0.17003563, 0.46843998, 0.92796258, 0.69814654, 0.41290051, 0.19561071, 0.16284783, 0.97016248, 0.71725408, 0.87702738, 0.31244595, 0.76615487, 0.20754036, 0.57871812, 0.07214068, 0.40356048, 0.12149553, 0.53222417, 0.9976855, 0.12536346, 0.80930099, 0.50962849, 0.94555126, 0.33364763]\n" + ] + } + ], + "source": [ + "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", + " [0.29960807, 0.42315023, 0.34452557, 0.4751035 ],\n", + " [0.17003563, 0.46843998, 0.92796258, 0.69814654],\n", + " [0.41290051, 0.19561071, 0.16284783, 0.97016248],\n", + " [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]])\n", + "list_numpy=a.tolist()\n", + "print(list_numpy)\n", + "numpy_elements= [value for tercia in list_numpy for value in tercia]\n", + "print(numpy_elements)" + ] }, { "cell_type": "markdown", @@ -89,10 +135,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.84062117, 0.7876326, 0.77109654, 0.81835917, 0.87645456, 0.7066597, 0.57433389, 0.92796258, 0.69814654, 0.97016248, 0.71725408, 0.87702738, 0.76615487, 0.57871812, 0.53222417, 0.9976855, 0.80930099, 0.50962849, 0.94555126]\n" + ] + } + ], + "source": [ + "greater_elements=[value for tercia in list_numpy for value in tercia if value >=0.5]\n", + "print(greater_elements)" + ] }, { "cell_type": "markdown", @@ -103,9 +160,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "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", + "[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" + ] + } + ], "source": [ "b = np.array([[[0.55867166, 0.06210792, 0.08147297],\n", " [0.82579068, 0.91512478, 0.06833034]],\n", @@ -120,7 +186,12 @@ " [0.8694668 , 0.65669313, 0.10708681]],\n", "\n", " [[0.07529684, 0.46470767, 0.47984544],\n", - " [0.65368638, 0.14901286, 0.23760688]]])" + " [0.65368638, 0.14901286, 0.23760688]]])\n", + "list_numpy2=b.tolist()\n", + "print(list_numpy2)\n", + "\n", + "numpy_elements2= [valor for sixvalues in list_numpy2 for three in sixvalues for valor in three]\n", + "print(numpy_elements2)\n" ] }, { @@ -153,10 +224,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['../data/r\\\\sample_file_0.txt', '../data/r\\\\sample_file_1.txt', '../data/r\\\\sample_file_2.txt', '../data/r\\\\sample_file_3.txt', '../data/r\\\\sample_file_4.txt', '../data/r\\\\sample_file_5.txt', '../data/r\\\\sample_file_6.txt', '../data/r\\\\sample_file_7.txt', '../data/r\\\\sample_file_8.txt', '../data/r\\\\sample_file_9.txt']\n" + ] + } + ], + "source": [ + "CSV_files=[os.path.join(\"../data/r\", file) for file in os.listdir(\"../data/\") if file.endswith(\"txt\")]\n", + "print(CSV_files)" + ] }, { "cell_type": "markdown", @@ -167,10 +249,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " 0 1 2 3 4 5 6 \\\n", + "0 0.734751 0.195362 0.734309 0.598184 0.763433 0.263434 0.868066 \n", + "1 0.772607 0.445391 0.249642 0.787922 0.598583 0.827238 0.624126 \n", + "2 0.226428 0.268764 0.694262 0.622335 0.063843 0.122683 0.815625 \n", + "3 0.362748 0.495430 0.113876 0.594149 0.612522 0.625204 0.864050 \n", + "4 0.033415 0.340433 0.464971 0.363737 0.025815 0.434129 0.415163 \n", + "\n", + " 7 8 9 10 11 12 13 \\\n", + "0 0.058092 0.753502 0.587513 0.311608 0.178356 0.182922 0.147631 \n", + "1 0.601524 0.688753 0.338870 0.081595 0.471474 0.267443 0.453351 \n", + "2 0.584542 0.032594 0.589775 0.764350 0.650973 0.565705 0.691784 \n", + "3 0.260279 0.528873 0.168043 0.715929 0.677014 0.175735 0.632370 \n", + "4 0.892210 0.381701 0.415264 0.790801 0.696930 0.819751 0.944029 \n", + "\n", + " 14 15 16 17 18 19 \n", + "0 0.391188 0.816049 0.749068 0.293260 0.937828 0.880858 \n", + "1 0.800716 0.045749 0.683793 0.389789 0.016787 0.503695 \n", + "2 0.265223 0.739031 0.560394 0.334802 0.517694 0.646110 \n", + "3 0.926715 0.085675 0.120525 0.141746 0.771144 0.489660 \n", + "4 0.869965 0.041723 0.819140 0.676051 0.109349 0.872947 \n" + ] + } + ], + "source": [ + "rutas=['../data/sample_file_0.txt']\n", + "dfs=[pd.read_csv(ruta) for ruta in rutas]\n", + "data=pd.concat(dfs)\n", + "print(data)" + ] }, { "cell_type": "markdown", @@ -181,10 +295,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.3627479843783245, 0.3404325433462202, 0.4649714815238164, 0.4341291641280539, 0.4152638711413228, 0.267442803433069, 0.0856753003374878, 0.3348015507267528]\n" + ] + } + ], + "source": [ + "med=[median for median in dfs[0].median() if median <=0.48]\n", + "print(med)" + ] }, { "cell_type": "markdown", @@ -217,7 +342,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -231,9 +356,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.7" + "version": "3.9.12" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 }