{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "1. Read a file from a csv file stored in your computer.\n", "\n", "`import csv\n", "reader = csv.reader(open('filename.csv', 'r'))`\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import csv\n", "reader = csv.reader(open('filename.csv', 'r'))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. Create a list of rows. \n", "\n", "When iterating the object reader, it converts each row into a list\n", "\n", "`???\n", "for rowList in reader:\n", " ????\n", "print (listA)`\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[['joao', '22'], ['maria', '21'], ['manuela', '23'], ['antonio', '22']]\n" ] } ], "source": [ "listA = []\n", "for rowList in reader:\n", " listA.append(rowList)\n", "print(listA)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. The result is similar to `[['joao', '22'], ['maria', '21'], ['manuela', '23'], ['antonio', '22']]`. So you have to convert the age from strings to integers or floats. If you use the function len it will gives you the number of rows." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['joao', 22], ['maria', 21], ['manuela', 23], ['antonio', 22]]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "for pair in listA:\n", " pair[1] = int(pair[1])\n", "listA" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. What is the average age of the students of the list?" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "22.0\n" ] } ], "source": [ "Sum = 0\n", "for pair in listA:\n", " Sum += pair[1]\n", "avg = Sum/len(listA)\n", "print(avg)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "22.0" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list1 = []\n", "list2 = []\n", "for i in listA:\n", " list1.append(i[0])\n", " list2.append(i[1])\n", "list2 \n", "a= sum(list2)/len(list2)\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5. What is the age of the older student?" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "23" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ages = [pair[1] for pair in listA]\n", "max(ages)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "6. Who is the older student? (suppose that ther is only on)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "manuela\n" ] } ], "source": [ "for a in listA:\n", " if a[1]==max(ages):\n", " print(a[0])" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'manuela'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "listA[ages.index(max(ages))][0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.0" } }, "nbformat": 4, "nbformat_minor": 4 }