{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Esercizio casa (1): Utilizzando la list Comprehension derivare una lista contente le emails." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['7562463@stud.units.it', '4846288@stud.units.it', '3585438@stud.units.it', '6223132@stud.units.it', '2387614@stud.units.it', '5936796@stud.units.it', '5489954@stud.units.it', '4335157@stud.units.it', '8297123@stud.units.it', '6851119@stud.units.it', '4896918@stud.units.it', '4173839@stud.units.it', '3823846@stud.units.it', '1338353@stud.units.it', '7959191@stud.units.it', '8869513@stud.units.it', '6442125@stud.units.it', '3866183@stud.units.it', '3292541@stud.units.it', '5493828@stud.units.it', '9264746@stud.units.it', '9871682@stud.units.it', '9182169@stud.units.it', '7289682@stud.units.it', '3868873@stud.units.it', '5762436@stud.units.it', '7311163@stud.units.it', '1687249@stud.units.it', '2557543@stud.units.it', '2735899@stud.units.it', '5578145@stud.units.it', '3447561@stud.units.it', '3653891@stud.units.it', '3364613@stud.units.it', '1788232@stud.units.it', '6968988@stud.units.it', '8981496@stud.units.it', '6216514@stud.units.it', '9374531@stud.units.it', '6181891@stud.units.it', '7545924@stud.units.it', '9298293@stud.units.it', '8728993@stud.units.it', '5385979@stud.units.it', '2371794@stud.units.it', '3342131@stud.units.it', '1749661@stud.units.it', '5999134@stud.units.it', '3193872@stud.units.it', '1332126@stud.units.it', '7427355@stud.units.it', '3798998@stud.units.it', '8326857@stud.units.it', '8923429@stud.units.it', '3392699@stud.units.it', '7968626@stud.units.it', '4334836@stud.units.it', '1447558@stud.units.it', '8281522@stud.units.it', '7994522@stud.units.it', '6133137@stud.units.it', '3573155@stud.units.it', '9587546@stud.units.it', '7939233@stud.units.it', '2954373@stud.units.it', '1339736@stud.units.it', '3684568@stud.units.it', '6689317@stud.units.it']\n" ] } ], "source": [ "#la soluzione รจ simile a come abbiamo derivato nomi_cognomi solo che dobbiamo usale la LC.\n", "#salvate le email un una lista chiamata emails\n", "import csv\n", "with open('files/studenti.txt', 'r') as csvfile:\n", " reader = csv.reader(csvfile,delimiter=';')\n", " emails=[row[-1] for row in reader][1:]\n", "print(emails)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['7562463@stud.units.it', '4846288@stud.units.it', '3585438@stud.units.it', '6223132@stud.units.it', '2387614@stud.units.it', '5936796@stud.units.it', '5489954@stud.units.it', '4335157@stud.units.it', '8297123@stud.units.it', '6851119@stud.units.it', '4896918@stud.units.it', '4173839@stud.units.it', '3823846@stud.units.it', '1338353@stud.units.it', '7959191@stud.units.it', '8869513@stud.units.it', '6442125@stud.units.it', '3866183@stud.units.it', '3292541@stud.units.it', '5493828@stud.units.it', '9264746@stud.units.it', '9871682@stud.units.it', '9182169@stud.units.it', '7289682@stud.units.it', '3868873@stud.units.it', '5762436@stud.units.it', '7311163@stud.units.it', '1687249@stud.units.it', '2557543@stud.units.it', '2735899@stud.units.it', '5578145@stud.units.it', '3447561@stud.units.it', '3653891@stud.units.it', '3364613@stud.units.it', '1788232@stud.units.it', '6968988@stud.units.it', '8981496@stud.units.it', '6216514@stud.units.it', '9374531@stud.units.it', '6181891@stud.units.it', '7545924@stud.units.it', '9298293@stud.units.it', '8728993@stud.units.it', '5385979@stud.units.it', '2371794@stud.units.it', '3342131@stud.units.it', '1749661@stud.units.it', '5999134@stud.units.it', '3193872@stud.units.it', '1332126@stud.units.it', '7427355@stud.units.it', '3798998@stud.units.it', '8326857@stud.units.it', '8923429@stud.units.it', '3392699@stud.units.it', '7968626@stud.units.it', '4334836@stud.units.it', '1447558@stud.units.it', '8281522@stud.units.it', '7994522@stud.units.it', '6133137@stud.units.it', '3573155@stud.units.it', '9587546@stud.units.it', '7939233@stud.units.it', '2954373@stud.units.it', '1339736@stud.units.it', '3684568@stud.units.it', '6689317@stud.units.it']\n" ] } ], "source": [ "with open('files/studenti.txt', 'r') as csvfile:\n", " emails = [riga.strip().split(';')[-1] for riga in csvfile][1:]\n", "print(emails)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Join : concatenazione multipla\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7562463@stud.units.it,4846288@stud.units.it,3585438@stud.units.it,6223132@stud.units.it,2387614@stud.units.it,5936796@stud.units.it,5489954@stud.units.it,4335157@stud.units.it,8297123@stud.units.it,6851119@stud.units.it,4896918@stud.units.it,4173839@stud.units.it,3823846@stud.units.it,1338353@stud.units.it,7959191@stud.units.it,8869513@stud.units.it,6442125@stud.units.it,3866183@stud.units.it,3292541@stud.units.it,5493828@stud.units.it,9264746@stud.units.it,9871682@stud.units.it,9182169@stud.units.it,7289682@stud.units.it,3868873@stud.units.it,5762436@stud.units.it,7311163@stud.units.it,1687249@stud.units.it,2557543@stud.units.it,2735899@stud.units.it,5578145@stud.units.it,3447561@stud.units.it,3653891@stud.units.it,3364613@stud.units.it,1788232@stud.units.it,6968988@stud.units.it,8981496@stud.units.it,6216514@stud.units.it,9374531@stud.units.it,6181891@stud.units.it,7545924@stud.units.it,9298293@stud.units.it,8728993@stud.units.it,5385979@stud.units.it,2371794@stud.units.it,3342131@stud.units.it,1749661@stud.units.it,5999134@stud.units.it,3193872@stud.units.it,1332126@stud.units.it,7427355@stud.units.it,3798998@stud.units.it,8326857@stud.units.it,8923429@stud.units.it,3392699@stud.units.it,7968626@stud.units.it,4334836@stud.units.it,1447558@stud.units.it,8281522@stud.units.it,7994522@stud.units.it,6133137@stud.units.it,3573155@stud.units.it,9587546@stud.units.it,7939233@stud.units.it,2954373@stud.units.it,1339736@stud.units.it,3684568@stud.units.it,6689317@stud.units.it\n" ] } ], "source": [ "#provate a derivare un stringa di emails, intervallate da una virgola utilizzando il comando join... \n", "# del tipo email1,email2,email3,...\n", "print(','.join(emails))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Esercizio casa (2)\n", "prendere la lista di tuple \"nomi_cognomi\" e determinare la lista dei nomi " ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['XMBiwms', 'rcPMIiQ', 'LviSCTe', 'qhfWBlk', 'EpgCbje', 'FnTmyLw', 'DsxvGsO', 'kgEpoNz', 'pHbsmuW', 'rxBKyiC', 'VryEfDB', 'zdeTxUi', 'XCDqybx', 'utgCFqU', 'SxLYUrB', 'ZKVcEKF', 'uerxhDd', 'kvXzGGl', 'YfmSLLW', 'lljKPvK', 'AzUchqD', 'qgWcdDY', 'kRTdFZi', 'yQdCfJt', 'JVKZZfG', 'flMGpci', 'cQOdFWd', 'RIMnsJK', 'rLjMRQr', 'fiGNKZn', 'QicfhWR', 'wjlZFha', 'ZcCHzlu', 'TEAiwSy', 'IReYOMP', 'UssYnrZ', 'qrRaMJr', 'nIQyIXZ', 'GibsKUm', 'htEsiZp', 'LWmqaFi', 'sXQjeeV', 'BXtlJjS', 'zvotiar', 'PDwKAji', 'dQgJnJe', 'CwLEQxj', 'GdNxUhI', 'uaDwefv', 'aPHhqyp', 'WjlffZc', 'aBwyeIZ', 'ShGeRvG', 'hxpRVka', 'tgMHKad', 'knPSQIf', 'soqxLPj', 'OlIfDlo', 'JeMpMpi', 'kVOwdAt', 'dLCbyWU', 'PHziriN', 'FDysIut', 'cvBQpZl', 'yKeRLcY', 'jRbzNER', 'EPMKdGk', 'AyBzGpf']\n" ] } ], "source": [ "#iscr_matricola;iscr_cognome;iscr_nome;email(matricola@stud.units.it)\n", "import csv\n", "with open('files/studenti.txt', 'r') as csvfile:\n", " reader = csv.reader(csvfile,delimiter=';')\n", " cognomi_nomi=[(row[1],row[2]) for row in reader][1:]\n", "#print(cognomi_nomi)\n", "\n", "nomi=[x[1] for x in cognomi_nomi]\n", "print(nomi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Esercizio casa (3)\n", "prendere la lista di tuple \"nomi_cognomi\" e determinare la lista contenete le stringhe\n", "\"Lo studente {nome} {cognome} ha la mail: {mail}, per esempio lo studente GfrdfG dsfre ha la mail: 123234@stud.units.it" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Lo studente XMBiwms CquFcxk ha la mail: 7562463@stud.units.it\n", "Lo studente rcPMIiQ qdtWVQD ha la mail: 4846288@stud.units.it\n", "Lo studente LviSCTe aorVdIB ha la mail: 3585438@stud.units.it\n", "Lo studente qhfWBlk mJilKul ha la mail: 6223132@stud.units.it\n", "Lo studente EpgCbje BCsegMd ha la mail: 2387614@stud.units.it\n", "Lo studente FnTmyLw DjcTjns ha la mail: 5936796@stud.units.it\n", "Lo studente DsxvGsO kPdhUyK ha la mail: 5489954@stud.units.it\n", "Lo studente kgEpoNz eeNvmpz ha la mail: 4335157@stud.units.it\n", "Lo studente pHbsmuW cFRmlCh ha la mail: 8297123@stud.units.it\n", "Lo studente rxBKyiC gDWJIyf ha la mail: 6851119@stud.units.it\n", "Lo studente VryEfDB mAwyhUv ha la mail: 4896918@stud.units.it\n", "Lo studente zdeTxUi UbaXTAi ha la mail: 4173839@stud.units.it\n", "Lo studente XCDqybx ETSTerz ha la mail: 3823846@stud.units.it\n", "Lo studente utgCFqU QHwqWEL ha la mail: 1338353@stud.units.it\n", "Lo studente SxLYUrB QanjPCO ha la mail: 7959191@stud.units.it\n", "Lo studente ZKVcEKF DDcKqrS ha la mail: 8869513@stud.units.it\n", "Lo studente uerxhDd BAXWZfF ha la mail: 6442125@stud.units.it\n", "Lo studente kvXzGGl AHAYCVK ha la mail: 3866183@stud.units.it\n", "Lo studente YfmSLLW EQqwluD ha la mail: 3292541@stud.units.it\n", "Lo studente lljKPvK cbPRigB ha la mail: 5493828@stud.units.it\n", "Lo studente AzUchqD FpxduZI ha la mail: 9264746@stud.units.it\n", "Lo studente qgWcdDY uuZjMdl ha la mail: 9871682@stud.units.it\n", "Lo studente kRTdFZi SqIQQmI ha la mail: 9182169@stud.units.it\n", "Lo studente yQdCfJt KcsItgn ha la mail: 7289682@stud.units.it\n", "Lo studente JVKZZfG WMMgqYs ha la mail: 3868873@stud.units.it\n", "Lo studente flMGpci XwcPBnj ha la mail: 5762436@stud.units.it\n", "Lo studente cQOdFWd RAlMPuJ ha la mail: 7311163@stud.units.it\n", "Lo studente RIMnsJK jqLqrxA ha la mail: 1687249@stud.units.it\n", "Lo studente rLjMRQr FOaOJsQ ha la mail: 2557543@stud.units.it\n", "Lo studente fiGNKZn xlOdmMY ha la mail: 2735899@stud.units.it\n", "Lo studente QicfhWR MFDNeYx ha la mail: 5578145@stud.units.it\n", "Lo studente wjlZFha EDJUios ha la mail: 3447561@stud.units.it\n", "Lo studente ZcCHzlu FZzHSfS ha la mail: 3653891@stud.units.it\n", "Lo studente TEAiwSy UwgMNGr ha la mail: 3364613@stud.units.it\n", "Lo studente IReYOMP ZnGfkDI ha la mail: 1788232@stud.units.it\n", "Lo studente UssYnrZ nMFUrmn ha la mail: 6968988@stud.units.it\n", "Lo studente qrRaMJr PobMPRJ ha la mail: 8981496@stud.units.it\n", "Lo studente nIQyIXZ rqsXdyR ha la mail: 6216514@stud.units.it\n", "Lo studente GibsKUm MEmyHDx ha la mail: 9374531@stud.units.it\n", "Lo studente htEsiZp YNpWjEC ha la mail: 6181891@stud.units.it\n", "Lo studente LWmqaFi xarnBSj ha la mail: 7545924@stud.units.it\n", "Lo studente sXQjeeV tcnChzR ha la mail: 9298293@stud.units.it\n", "Lo studente BXtlJjS JHkWpSr ha la mail: 8728993@stud.units.it\n", "Lo studente zvotiar TegbCUA ha la mail: 5385979@stud.units.it\n", "Lo studente PDwKAji XfbwIcn ha la mail: 2371794@stud.units.it\n", "Lo studente dQgJnJe fzYphiG ha la mail: 3342131@stud.units.it\n", "Lo studente CwLEQxj iJyzekR ha la mail: 1749661@stud.units.it\n", "Lo studente GdNxUhI xhOUWVu ha la mail: 5999134@stud.units.it\n", "Lo studente uaDwefv euWXflA ha la mail: 3193872@stud.units.it\n", "Lo studente aPHhqyp jIXcCCC ha la mail: 1332126@stud.units.it\n", "Lo studente WjlffZc ZWffwrx ha la mail: 7427355@stud.units.it\n", "Lo studente aBwyeIZ QaNFkbH ha la mail: 3798998@stud.units.it\n", "Lo studente ShGeRvG ADAOhoQ ha la mail: 8326857@stud.units.it\n", "Lo studente hxpRVka nDpuUMD ha la mail: 8923429@stud.units.it\n", "Lo studente tgMHKad PTfaZuj ha la mail: 3392699@stud.units.it\n", "Lo studente knPSQIf xxhAqsb ha la mail: 7968626@stud.units.it\n", "Lo studente soqxLPj RZRifMr ha la mail: 4334836@stud.units.it\n", "Lo studente OlIfDlo ZVOAzMr ha la mail: 1447558@stud.units.it\n", "Lo studente JeMpMpi AyrmLoZ ha la mail: 8281522@stud.units.it\n", "Lo studente kVOwdAt hixngHH ha la mail: 7994522@stud.units.it\n", "Lo studente dLCbyWU BgJhnKW ha la mail: 6133137@stud.units.it\n", "Lo studente PHziriN wCqjUvO ha la mail: 3573155@stud.units.it\n", "Lo studente FDysIut UyWSDtp ha la mail: 9587546@stud.units.it\n", "Lo studente cvBQpZl CHYCwZd ha la mail: 7939233@stud.units.it\n", "Lo studente yKeRLcY OMdMMNj ha la mail: 2954373@stud.units.it\n", "Lo studente jRbzNER dDSdTQB ha la mail: 1339736@stud.units.it\n", "Lo studente EPMKdGk TAzbxYk ha la mail: 3684568@stud.units.it\n", "Lo studente AyBzGpf FoFRkRn ha la mail: 6689317@stud.units.it\n" ] } ], "source": [ "#suggerimento dobbiamo determinare una lista .... quindi penso subito alla LC... , devo scrivere alcune cose \n", "#dentro altre stringhe in posti ben precisi... quindi mi viene in mente strong.format\n", "with open('files/studenti.txt', 'r') as csvfile:\n", " reader = csv.reader(csvfile,delimiter=';')\n", " next(reader)\n", " res=['Lo studente {} {} ha la mail: {}'.format(x[2],x[1],x[-1]) for x in reader]\n", "print('\\n'.join(res))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[('xhOUWVu', 'GdNxUhI'), ('OMdMMNj', 'yKeRLcY'), ('QaNFkbH', 'aBwyeIZ'), ('KcsItgn', 'yQdCfJt'), ('EDJUios', 'wjlZFha'), ('hixngHH', 'kVOwdAt'), ('dDSdTQB', 'jRbzNER'), ('UyWSDtp', 'FDysIut'), ('ETSTerz', 'XCDqybx'), ('cbPRigB', 'lljKPvK'), ('YNpWjEC', 'htEsiZp'), ('nMFUrmn', 'UssYnrZ'), ('mJilKul', 'qhfWBlk'), ('qdtWVQD', 'rcPMIiQ'), ('BgJhnKW', 'dLCbyWU'), ('DDcKqrS', 'ZKVcEKF'), ('iJyzekR', 'CwLEQxj'), ('CquFcxk', 'XMBiwms'), ('kPdhUyK', 'DsxvGsO'), ('euWXflA', 'uaDwefv'), ('RAlMPuJ', 'cQOdFWd'), ('fzYphiG', 'dQgJnJe'), ('XwcPBnj', 'flMGpci'), ('nDpuUMD', 'hxpRVka'), ('ADAOhoQ', 'ShGeRvG'), ('TAzbxYk', 'EPMKdGk'), ('aorVdIB', 'LviSCTe'), ('gDWJIyf', 'rxBKyiC'), ('jqLqrxA', 'RIMnsJK'), ('EQqwluD', 'YfmSLLW'), ('AyrmLoZ', 'JeMpMpi'), ('ZVOAzMr', 'OlIfDlo'), ('UwgMNGr', 'TEAiwSy'), ('UbaXTAi', 'zdeTxUi'), ('PobMPRJ', 'qrRaMJr'), ('xxhAqsb', 'knPSQIf'), ('QHwqWEL', 'utgCFqU'), ('FpxduZI', 'AzUchqD'), ('AHAYCVK', 'kvXzGGl'), ('rqsXdyR', 'nIQyIXZ'), ('jIXcCCC', 'aPHhqyp'), ('MEmyHDx', 'GibsKUm'), ('ZWffwrx', 'WjlffZc'), ('xlOdmMY', 'fiGNKZn'), ('DjcTjns', 'FnTmyLw'), ('eeNvmpz', 'kgEpoNz'), ('cFRmlCh', 'pHbsmuW'), ('FoFRkRn', 'AyBzGpf'), ('PTfaZuj', 'tgMHKad'), ('tcnChzR', 'sXQjeeV'), ('JHkWpSr', 'BXtlJjS'), ('xarnBSj', 'LWmqaFi'), ('CHYCwZd', 'cvBQpZl'), ('FOaOJsQ', 'rLjMRQr'), ('ZnGfkDI', 'IReYOMP'), ('SqIQQmI', 'kRTdFZi'), ('MFDNeYx', 'QicfhWR'), ('uuZjMdl', 'qgWcdDY'), ('mAwyhUv', 'VryEfDB'), ('BCsegMd', 'EpgCbje'), ('QanjPCO', 'SxLYUrB'), ('RZRifMr', 'soqxLPj'), ('wCqjUvO', 'PHziriN'), ('BAXWZfF', 'uerxhDd'), ('TegbCUA', 'zvotiar'), ('FZzHSfS', 'ZcCHzlu'), ('WMMgqYs', 'JVKZZfG'), ('XfbwIcn', 'PDwKAji')]\n" ] } ], "source": [ "#cosa fa questo codice\n", "import random\n", "def cosaFa(cognomi_nomi):\n", " return [cognomi_nomi.pop(random.randint(0,len(cognomi_nomi)-1)) for i in range(len(cognomi_nomi))] \n", "\n", "cognomi_nomi = cosaFa(cognomi_nomi)\n", "print(cognomi_nomi)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[('rqsXdyR', 'nIQyIXZ'), ('fzYphiG', 'dQgJnJe'), ('xarnBSj', 'LWmqaFi'), ('cFRmlCh', 'pHbsmuW'), ('nMFUrmn', 'UssYnrZ'), ('FoFRkRn', 'AyBzGpf'), ('WMMgqYs', 'JVKZZfG'), ('BAXWZfF', 'uerxhDd'), ('uuZjMdl', 'qgWcdDY'), ('qdtWVQD', 'rcPMIiQ'), ('BCsegMd', 'EpgCbje'), ('jqLqrxA', 'RIMnsJK'), ('CquFcxk', 'XMBiwms'), ('CHYCwZd', 'cvBQpZl'), ('EDJUios', 'wjlZFha'), ('BgJhnKW', 'dLCbyWU'), ('TAzbxYk', 'EPMKdGk'), ('UyWSDtp', 'FDysIut'), ('FOaOJsQ', 'rLjMRQr'), ('QaNFkbH', 'aBwyeIZ'), ('KcsItgn', 'yQdCfJt'), ('XwcPBnj', 'flMGpci'), ('ZWffwrx', 'WjlffZc'), ('QanjPCO', 'SxLYUrB'), ('QHwqWEL', 'utgCFqU'), ('AyrmLoZ', 'JeMpMpi'), ('mAwyhUv', 'VryEfDB'), ('eeNvmpz', 'kgEpoNz'), ('DDcKqrS', 'ZKVcEKF'), ('AHAYCVK', 'kvXzGGl'), ('xlOdmMY', 'fiGNKZn'), ('PTfaZuj', 'tgMHKad'), ('OMdMMNj', 'yKeRLcY'), ('EQqwluD', 'YfmSLLW'), ('ZVOAzMr', 'OlIfDlo'), ('FpxduZI', 'AzUchqD'), ('aorVdIB', 'LviSCTe'), ('RZRifMr', 'soqxLPj'), ('mJilKul', 'qhfWBlk'), ('ADAOhoQ', 'ShGeRvG'), ('FZzHSfS', 'ZcCHzlu'), ('UbaXTAi', 'zdeTxUi'), ('xxhAqsb', 'knPSQIf'), ('PobMPRJ', 'qrRaMJr'), ('XfbwIcn', 'PDwKAji'), ('gDWJIyf', 'rxBKyiC'), ('SqIQQmI', 'kRTdFZi'), ('UwgMNGr', 'TEAiwSy'), ('euWXflA', 'uaDwefv'), ('ETSTerz', 'XCDqybx'), ('hixngHH', 'kVOwdAt'), ('JHkWpSr', 'BXtlJjS'), ('nDpuUMD', 'hxpRVka'), ('xhOUWVu', 'GdNxUhI'), ('dDSdTQB', 'jRbzNER'), ('YNpWjEC', 'htEsiZp'), ('MEmyHDx', 'GibsKUm'), ('cbPRigB', 'lljKPvK'), ('iJyzekR', 'CwLEQxj'), ('wCqjUvO', 'PHziriN'), ('kPdhUyK', 'DsxvGsO'), ('jIXcCCC', 'aPHhqyp'), ('TegbCUA', 'zvotiar'), ('tcnChzR', 'sXQjeeV'), ('RAlMPuJ', 'cQOdFWd'), ('MFDNeYx', 'QicfhWR'), ('ZnGfkDI', 'IReYOMP'), ('DjcTjns', 'FnTmyLw')]\n" ] } ], "source": [ "#lo shuffle esiste nel pacchetto random!!! \n", "import random\n", "random.shuffle(cognomi_nomi)\n", "print(cognomi_nomi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Esercizio svolto:\n", "\n", "Crea una funzione \"listaSenzaDoppioni(file)\" che prende in ingresso una file e crea una lista ordinata di parole distinte\n" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "def listaSenzaDoppioni(file):\n", " with open(file) as f:\n", " s=f.read()\n", " sv=s.split()\n", " #print(sv)\n", " risposta=[]\n", " for c in sv:\n", " if c not in risposta:\n", " risposta.append(c) \n", " #risposta=list(set(sv))\n", " risposta.sort()\n", " return risposta" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Aenean',\n", " 'Aliquam',\n", " 'Cras',\n", " 'Curabitur',\n", " 'Donec',\n", " 'Duis',\n", " 'Etiam',\n", " 'Fusce',\n", " 'In',\n", " 'Integer',\n", " 'Lorem',\n", " 'Maecenas',\n", " 'Mauris',\n", " 'Morbi',\n", " 'Nam',\n", " 'Nulla',\n", " 'Nullam',\n", " 'Nunc',\n", " 'Phasellus',\n", " 'Praesent',\n", " 'Proin',\n", " 'Sed',\n", " 'Suspendisse',\n", " 'Vivamus',\n", " 'a',\n", " 'a,',\n", " 'a.',\n", " 'ac',\n", " 'accumsan',\n", " 'adipiscing',\n", " 'aliquam.',\n", " 'aliquet',\n", " 'amet,',\n", " 'amet.',\n", " 'ante',\n", " 'ante.',\n", " 'arcu',\n", " 'arcu.',\n", " 'at',\n", " 'at.',\n", " 'auctor,',\n", " 'auctor.',\n", " 'augue',\n", " 'augue,',\n", " 'bibendum',\n", " 'blandit.',\n", " 'commodo',\n", " 'condimentum',\n", " 'condimentum.',\n", " 'congue',\n", " 'congue.',\n", " 'consectetur',\n", " 'consectetur.',\n", " 'consequat',\n", " 'consequat,',\n", " 'convallis',\n", " 'convallis.',\n", " 'cursus',\n", " 'cursus,',\n", " 'dapibus',\n", " 'dapibus.',\n", " 'diam',\n", " 'diam,',\n", " 'diam.',\n", " 'dictum',\n", " 'dolor',\n", " 'dui',\n", " 'dui,',\n", " 'dui.',\n", " 'efficitur',\n", " 'egestas',\n", " 'eget',\n", " 'eleifend',\n", " 'elementum',\n", " 'elit',\n", " 'elit.',\n", " 'enim',\n", " 'erat',\n", " 'erat.',\n", " 'eros',\n", " 'eros,',\n", " 'eros.',\n", " 'est',\n", " 'et',\n", " 'eu',\n", " 'ex',\n", " 'facilisis',\n", " 'faucibus',\n", " 'felis',\n", " 'fermentum.',\n", " 'feugiat',\n", " 'finibus',\n", " 'fringilla',\n", " 'fringilla,',\n", " 'gravida',\n", " 'hendrerit',\n", " 'iaculis',\n", " 'id',\n", " 'imperdiet',\n", " 'in',\n", " 'interdum',\n", " 'ipsum',\n", " 'ipsum,',\n", " 'justo,',\n", " 'lacinia',\n", " 'lacinia,',\n", " 'lacus',\n", " 'lacus.',\n", " 'laoreet',\n", " 'laoreet.',\n", " 'lectus',\n", " 'leo',\n", " 'leo.',\n", " 'libero,',\n", " 'ligula',\n", " 'ligula,',\n", " 'ligula.',\n", " 'lobortis',\n", " 'lorem,',\n", " 'lorem.',\n", " 'luctus.',\n", " 'magna',\n", " 'magna,',\n", " 'magna.',\n", " 'malesuada,',\n", " 'massa',\n", " 'mattis',\n", " 'mauris',\n", " 'mauris,',\n", " 'maximus',\n", " 'maximus,',\n", " 'metus',\n", " 'metus.',\n", " 'mi',\n", " 'mi,',\n", " 'molestie',\n", " 'mollis',\n", " 'nec',\n", " 'nec.',\n", " 'nibh',\n", " 'nibh.',\n", " 'nisi',\n", " 'nisi.',\n", " 'nisl,',\n", " 'non',\n", " 'non,',\n", " 'non.',\n", " 'nulla',\n", " 'nunc',\n", " 'nunc,',\n", " 'odio',\n", " 'odio.',\n", " 'orci',\n", " 'orci.',\n", " 'ornare',\n", " 'ornare.',\n", " 'pellentesque',\n", " 'pharetra.',\n", " 'placerat',\n", " 'porta.',\n", " 'porttitor',\n", " 'posuere',\n", " 'potenti.',\n", " 'pretium.',\n", " 'pulvinar',\n", " 'purus',\n", " 'purus,',\n", " 'quam',\n", " 'quam,',\n", " 'quam.',\n", " 'quis',\n", " 'quis,',\n", " 'rhoncus',\n", " 'rutrum',\n", " 'rutrum,',\n", " 'sagittis',\n", " 'sagittis.',\n", " 'sapien',\n", " 'sapien,',\n", " 'sapien.',\n", " 'scelerisque',\n", " 'sed',\n", " 'sem',\n", " 'sem.',\n", " 'semper',\n", " 'sit',\n", " 'sodales',\n", " 'sodales.',\n", " 'sollicitudin',\n", " 'sollicitudin.',\n", " 'suscipit',\n", " 'suscipit.',\n", " 'tellus',\n", " 'tempor',\n", " 'tempor,',\n", " 'tempus',\n", " 'tincidunt',\n", " 'tortor',\n", " 'tortor,',\n", " 'tristique',\n", " 'tristique.',\n", " 'turpis',\n", " 'turpis,',\n", " 'ullamcorper',\n", " 'ullamcorper.',\n", " 'ultricies',\n", " 'urna',\n", " 'ut',\n", " 'ut,',\n", " 'varius',\n", " 'vehicula',\n", " 'vel',\n", " 'vel,',\n", " 'velit',\n", " 'venenatis',\n", " 'vitae',\n", " 'vitae.',\n", " 'viverra',\n", " 'volutpat',\n", " 'vulputate']" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "listaSenzaDoppioni('files/lorem.txt')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Esercizio a casa: Algoritmo di Euclide MCD" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "a b \n", "20 = 8*2 +4\n", "8 = 4*2 + 0\n", "4 = 0 + ..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#code here\n", "def MCD(a,b):\n", " while b != 0:\n", " a, b = b, a % b\n", " return a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "MCD(10,4)" ] } ], "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.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }