Está en la página 1de 34

9 Informática (Teorı́a)

Python: Listas → Diccionarios, Intro a Ficheros

Joaquim Gabarro
gabarro@cs.upc.edu

Computer Science
Universitat Politècnica de Catalunya
Listas→Diccionarios

Ficheros
Listas→Diccionarios
Ejemplo: Agenda

I Dada una lista que contiene como elementos listas con


información sobre personas:
[
["Juan", "610000001", "Balmes 111"],
["Pedro", "610000002", "Parı́s 222"]
]

I Construir un diccionario que tiene como claves los


nombres y como valores la información asociada:
{
’Juan’: [’610000001’, ’Balmes 111’],
’Pedro’: [’610000002’, ’Parı́s 222’]
}

Atención: suponemos que no hay nombres repetidos.


Diseño

1. Creamos el diccionario vacio, agenda = {}


2. Recorremos con x los elementos de lista para llenar
agenda.

Tenemos el esquema:

def crear_agenda(lista):
agenda = {}
for x in lista:
# llenar agenda
return agenda
Veamos como llenar agenda.
Sea x= ["Juan", "610000001", "Balmes 111"]
hay que añadir a agenda:
’Juan’: [’610000001’, ’Balmes 111’]
La clave es ’Juan’ el valor es [’610000001’,’Balmes 111’]
Obtenemos la clave y el valor a partir de x:

>>> x= ["Juan", "610000001", "Balmes 111"]


>>> clave=x[0]
>>> clave
’Juan’
>>> valor=x[1:]
>>> valor
[’610000001’, ’Balmes 111’]
Una primera versión crear agenda.py:
def crear_agenda(lista):
agenda = {}
for x in lista:
clave, valor = x[0], x[1:]
agenda[clave] = valor
return agenda

Más compacto crear agenda compacto.py:


def crear_agenda(lista):
agenda = {}
for x in lista:
agenda[x[0]] = x[1:]
return agenda

Pregunta: que pasa si hay dos Juan?


crear agenda doctest.py

Versión completa con doctest:


def crear_agenda(lista):
’’’
>>> crear_agenda([["Juan", "610000001", "Balmes 111"], ...])
{’Juan’: [’610000001’, ’Balmes 111’], ’Pedro’: [’610000002’, ’Parı́s 222’]}
’’’

agenda = {}
for x in lista:
agenda[x[0]] = x[1:] # cuidado si hay nombres repetidos
return agenda

if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
L = [["Juan", "610000001", "Balmes 111"],
["Pedro", "610000002", "Parı́s 222"],
["Antonio", "610000003", "Valencia 333"],
["Edu", "610000004", "Mallorca 444"]]
print("imprimir L: ", L)
a = crear_agenda(L)
print("agenda: ", a)
print("datos de Juan: ", a["Juan"])
print("telefono de Juan", a["Juan"][0])
Exemple: Inverted Index
https://en.wikipedia.org/wiki/Inverted_index
An inverted index (also referred to as postings file or inverted
file) is an index data structure storing a mapping from content,
such as words or numbers, to its locations in a database file...
Pag 387 of How to Think Like a Computer Scientist: Python 3
starts the following Index (really and Inverted Index)
A
abbreviated assignment,92
abecedarian series,116
abstract data type (ADT),323
accumulator,297
algorithm, 2,7, 106,107
...
B
baked animation,246
bar chart, 63
base case, 252,256
...
Let us consider the following toy exercise.

Given a text similar to the following one:


my text= "hola mola tic tac mola hola tic tic toc"

Build the following diccionary:


{’hola’:[0,5],’mola’:[1,4],’tic’:[2,6,7],’tac’:0[3],’toc’:[8]}
toy inverted file.py

def toy_inverted_index(text):
word=text.split()
inverted_file={}
for k in range(len(word)):
if word[k] in inverted_file:
inverted_file[word[k]].append(k)
else:
inverted_file[word[k]] = [k]
return inverted_file
Ejemplo: locos por los anagramas

https://es.wikipedia.org/wiki/Anagrama
https://es.wikipedia.org/wiki/Alfabeto_griego

Un anagrama (del griego ανα -ana-, prefijo que significa de


vuelta, y γραµµα -gramma-, letra) es una palabra o frase que
resulta de la transposición de letras de otra palabra o frase.

Ejemplo: ’roma’, ’amor’, ’mora’

Divertimento: Leer en griego con latex:


I La palabra ανα en latex se escribe
$\alpha\nu\alpha$-> a n a

I La palabra γραµµα se escribe:


$\gamma\rho\alpha\mu\mu\alpha$ -> g r a m m a
Decidir si dos palabras son anagramas:
1. Reordenar alfabeticamente cada palabra.
Recordemos que el typo str es immnutable por lo tanto
no podemos reordenar “in-place”.
>>> a = ’ZENOVW’
>>> b = sorted(a)
>>> b
[’E’, ’N’, ’O’, ’V’, ’W’, ’Z’]
>>> c = "".join(b)
>>> c
’ENOVWZ’

Más corto (y crı́ptico)


>>> c="".join(sorted(a))
>>> c
’ENOVWZ’

2. Mirar si son iguales las dos versiones reordenadas.


>>> "".join(sorted("roma")) == "".join(sorted("amor"))
True
Ejercicio: locos por los anagramas

Dada una lista inicial de palabras:


[’roma’,’arbol’,’labor’,’genet’,’amor’,’mora’,’gente’,’uno’,’dos’]

Hay que construir un diccionario que agrupa los anagramas en


listas. Si una palabra no tienen ningún anagrama no ha de
aparecer.
{0:[’roma’,’amor’,’mora’],1:[’arbol’,’labor’],2:[’genet’,’gente’]}
Etapas de un posible diseño:
I Construir la lista de claves. Las claves son las palabras
ordenadas alfabeticamenteevitando repeticiones.
Ejemplo: la clave de ’genet’ y ’gente’ es ’eegnt’.
keys: [’amor’, ’ablor’, ’eegnt’, ’nou’, ’dos’]

I Construir el diccionario anagramas. Tiene como claves el


conjunto keys. Comenzamos con la versión sin
anagramas.
anagramas: {’ablor’:[],’nou’:[],’amor’:[],’eegnt’:[],’dos’:[]}
I Llenar anagramas.
anagramas: {
’ablor’: [’arbol’, ’labor’],
’nou’: [’uno’],
’amor’: [’roma’, ’amor’, ’mora’],
’eegnt’: [’genet’, ’gente’],
’dos’: [’dos’]
}

Hay claves con una sola palabra, hay que eliminarlas en la


versión final.
Ejemplo: ’nou’:[’uno’]
I Construir la versión final
numeros={0:[’roma’,’amor’,’mora’],1:[’arbol’,’labor’],...}
locos por anagramas.py

Dos funciones útiles:


def ordena(palabra):
return "".join(sorted(palabra))

def future_keys(L):
keys=[]
for w in L:
if ordena(w) not in keys:
keys.append(ordena(w))
return keys
def locos_por_anagramas(L):
# 1. conunto de claves
keys=future_keys(L)
# 2. dic anagramas vacio
anagramas={}
for key in keys:
anagramas[key]=[]
# 3. llenamos dic anagramas
for palabra in L:
clave = ordena(palabra)
anagramas[clave].append(palabra)
# 4. dic numeros: version final
numeros={}
i=0
for clave in anagramas:
if len(anagramas[clave])>1:
numeros[i]=anagramas[clave]
i=i+1
return numeros

En locos por anagramas extended.py teneis una versión


extendida en que se imprimen los datos paso a paso.
Ficheros
Files (Ficheros)

From Chapter 13 of the book (available in Atenea):

How to Think Like a Computer Scientist:


Learning with Python 3 Documentation”
What is a file?
I While a program is running, its data is stored in random
access memory (RAM) . RAM is volatile. When the
program ends, or the computer shuts down, data in RAM
disappears.
I To make data available the next time it has to be written to
a non-volatile storage medium, such a hard drive, usb
drive, or CD-RW.
I Data on non-volatile storage media is stored in named
locations called files. By reading and writing files,
programs can save information between runs.
I To use a file, it has to be opened. When done, it has to be
closed. While the file is open, it can either be read from or
written to. The holder knows where they are.
I To open a file, we specify its name and indicate whether
we want to read or write.
Example: Writing my first file

Let’s begin with a simple program my first write file.py


that writes three lines of text into a file:
def my_first_write_file(file_name):
myfile=open(file_name, "w")
myfile.write("My first file written from Python\n")
myfile.write("---------------------------------\n")
myfile.write("Hello, world!\n")
myfile.close()

def main():
my_first_write_file("my_first_file.txt")

main()
After executing:
>>>
RESTART: C:... \my_first_write_file.py
>>>

there is in the same forder a file my first file.txt


containing:
My first file written from Python
---------------------------------
Hello, world!
Comments
I Opening a file creates a file handle. In this case myfile.
I Calls on the handle, changes the file located on the disk.
I The open function takes two arguments:

myfile=open("my first file.txt", "w")

The first is the name of the file, the second is the mode.
I Mode "w" means that we are opening the file for writing.
I With mode "w", if there is no file my first file.txt, it
will be created. If there is one, it will be replaced by the file
we are writing.
I To put data in the file we invoke the write()
myfile.write("My first file written from Python\n"

I Closing the file handle tells the system that we are done
and makes the disk file available.
Example: Reading my first file

I Now that the file exists on our disk, we can open it,
I this time for reading,
I and read all the lines in the file, one at a time.
I This time, the mode argument is r for reading

Suppose there is a file my first file.txt containing:


My first file written from Python
---------------------------------
Hello, world!

Method readline() (in singular) try to read next line


my first read file.py
def my_first_read_file(file):
mynewhandle = open(file, "r")
theline = mynewhandle.readline() #Try to read next line
while len(theline)>0:
print("number of characters:",len(theline),theline, end="")
theline = mynewhandle.readline()
mynewhandle.close()

def main():
my_first_read_file("my_first_file.txt")

main()

This explains the end-of-file detection: when there are no more


lines to be read, readline returns an empty string — one that
does not even have a newline at the end, hence its length is 0.
>>> len("") # not even have a newline at the end
0
>>> len("\n") # just a new line at the end
1
The result of the execution is:
>>>
RESTART: C:....\my_first_read_file.py
number of characters: 34 My first file written from Python
number of characters: 34 ---------------------------------
number of characters: 14 Hello, world!
>>>
Example: Friends & Emails

Given the file my friends.txt containing our friends and


emails, one per line:
kim kim@intro.program.caos
maria mario@must.program.success
jorge jorge@python.fortran.linux
edelmira edelmira@c.plus.plus
jordi jordi@advocat.proves.compila

we’d like the lines sorted into alphabetical order


>>>
RESTART: C:\Users....\sort_friends.py
edelmira edelmira@c.plus.plus
jordi jordi@advocat.proves.compila
jorge jorge@python.fortran.linux
kim kim@intro.program.caos
maria mario@must.program.success
A good plan is:
I to read everything into a list of lines,
I then sort the list,
I and then write the sorted list back to another file

With a little more detail:


def sort_friends(my_friends):
f = open(my_friends, "r")
xs = #read all the lines of f and store into the list xs
f.close()
#use the sort() from lists to sort in place
g = open("sorted_friends.txt", "w")
for v in xs: #remind xs is a list
#write v into g
g.close()
File sort friends.py contains:

def sort_friends(my_friends):
f = open(my_friends, "r")
xs = f.readlines()
f.close()
xs.sort()
g = open("sorted_friends.txt", "w")
for v in xs:
g.write(v)
g.close()

The readlines() reads all the lines and returns a list of strings:
[’kim kim@intro.program.caos\n’, ’maria mario@must.program.success\n’,
’jorge jorge@python.fortran.linux\n’, ’edelmira edelmira@c.plus.plus\n’,
’jordi jordi@advocat.proves.compila\n’]
To run the program we also need:

def my_read_file(file):
mynewhandle = open(file, "r")
theline = mynewhandle.readline()
while len(theline)>0:
print(theline, end="")
theline = mynewhandle.readline()
mynewhandle.close()

def main():
sort_friends("my_friends.txt")
my_read_file("sorted_friends.txt")
Executing we get:
>>>
RESTART: C:\Users...\sort_friends.py
edelmira edelmira@c.plus.plus
jordi jordi@advocat.proves.compila
jorge jorge@python.fortran.linux
kim kim@intro.program.caos
maria mario@must.program.success
>>>
Following sort friends extended.py.
Some prints() are added to trace the program’s evolution
def sort_friends(my_friends):
f = open(my_friends, "r")
xs = f.readlines()
print("\n--- a list ----\n")
print(xs)
f.close()
print("\n--- the sorted list ----\n")
xs.sort()
print(xs)
g = open("sorted_friends.txt", "w")
for v in xs:
g.write(v)
g.close()

def my_read_file(file):
mynewhandle = open(file, "r")
theline = mynewhandle.readline()
print("\n--- print the new file ----\n")
while len(theline)>0:
print(theline, end="")
theline = mynewhandle.readline()
mynewhandle.close()

def main():
sort_friends("my_friends.txt")
my_read_file("sorted_friends.txt")
RESTART: C:...\sort_friends_extended.py

--- a list ----

[’kim kim@intro.program.caos\n’, ’maria mario@must.program.success\n’,


’jorge jorge@python.fortran.linux\n’, ’edelmira edelmira@c.plus.plus\n’,
’jordi jordi@advocat.proves.compila\n’]

--- the sorted list ----

[’edelmira edelmira@c.plus.plus\n’, ’jordi jordi@advocat.proves.compila\n’,


’jorge jorge@python.fortran.linux\n’, ’kim kim@intro.program.caos\n’,
’maria mario@must.program.success\n’]

--- print the new file ----

edelmira edelmira@c.plus.plus
jordi jordi@advocat.proves.compila
jorge jorge@python.fortran.linux
kim kim@intro.program.caos
maria mario@must.program.success
>>>

También podría gustarte