Skip to main content

Ordering lists... stored in a txt file

Ok, this time we want to take the list of names from a file.
Let's say we have this names written in a txt file called

Agresta Lenza
Guglielmo Starace
Filomena Cesareo
Annarita Cortazzo
Pasqualina Amoresano
Tiziana Iuliano
Fortunata Mangia
Matteo Cavallo
Angela Imbriaco
Ortensia Russo
Marotta Sansone
Maurizio De Vita
Caputo Califano
Annarita Rambaldi
Sara Piciocchi
Marotta La Marca
Ida Franco
Giovanni Saturno
Rosalba Boselli
Giuseppe Rossi
Lucia Pirone
Flavio Rosciano
Maria Mangia
Guglielmo De Vita
Guglielmo Balzano
Paola Russo
Flavia Turriziani
Pasqualina Renato
Rosalba Starace
Ida Piccione
Gaetana Cortazzo
Anna Starace
Olga Russo
Giovanni Cortazzo
Annapina Rubano
Caruccio De Angelis
Olga La Marca
Mariagrazia La Regina
Anella Piccione
Annarita Passaro
Angelamaria Rosaria Cozza
Agresta Orco
Grazia Turriziani
Flavio De Luca
Annapina Russo
Annarita Rattazzi
Dario Luongo
Michele De Angelis
Rosalba Cortazzo
Pasqualina Rossi

Now, like in the previous post we want to order them by the surname...
We have to read first the file this way


---------------------------- code -----------------------
with open("ElencoCorsisti.txt") as corsisti:
    listofnames = corsisti.readlines()

# Then we do exactly what we did before:

wholename = [] # we will store here the surname + name
for names in listofnames:
    splitted = names.split() # let's split the name
    if len(splitted) == 3:
        if splitted[1] == "De" or splitted[1]== "La":
            wholename.append(splitted[1] + " " + splitted[2] + " " + splitted[0])
        else:
            wholename.append(splitted[2] + " " + splitted[0] + " " + splitted[1])
    elif len(splitted) == 4:
        if splitted[2] == "De" or splitted[1]== "La": # We have 2 names and 2 surnames
            wholename.append(splitted[2] + " " + splitted[3] + " " + splitted[0] + " " + splitted[1])
        else:
            wholename.append(splitted[3] + " " + splitted[0] + " " + splitted[1] + " " + splitted [2])
    else:
   wholename.append(splitted[1] + " " +  splitted[0])

wholename.sort()
for name in wholename:
    print(wholename.index(name),")",name)

# The result:
------------------------------------------------

0 ) Amoresano Pasqualina
1 ) Balzano Guglielmo
2 ) Boselli Rosalba
3 ) Califano Caputo
4 ) Cavallo Matteo
5 ) Cesareo Filomena
6 ) Cortazzo Annarita
7 ) Cortazzo Gaetana
8 ) Cortazzo Giovanni
9 ) Cortazzo Rosalba
10 ) Cozza Angelamaria Rosaria
11 ) De Angelis Caruccio
12 ) De Angelis Michele
13 ) De Luca Flavio
14 ) De Vita Guglielmo
15 ) De Vita Maurizio
16 ) Franco Ida
17 ) Imbriaco Angela
18 ) Iuliano Tiziana
19 ) La Marca Marotta
20 ) La Marca Olga
21 ) La Regina Mariagrazia
22 ) Lenza Agresta
23 ) Luongo Dario
24 ) Mangia Fortunata
25 ) Mangia Maria
26 ) Orco Agresta
27 ) Passaro Annarita
28 ) Piccione Anella
29 ) Piccione Ida
30 ) Piciocchi Sara
31 ) Pirone Lucia
32 ) Rambaldi Annarita
33 ) Rattazzi Annarita
34 ) Renato Pasqualina
35 ) Rosciano Flavio
36 ) Rossi Giuseppe
37 ) Rossi Pasqualina
38 ) Rubano Annapina
39 ) Russo Annapina
40 ) Russo Olga
41 ) Russo Ortensia
42 ) Russo Paola
43 ) Sansone Marotta
44 ) Saturno Giovanni
45 ) Starace Anna
46 ) Starace Guglielmo
47 ) Starace Rosalba
48 ) Turriziani Flavia
49 ) Turriziani Grazia

Comments

Popular posts from this blog

Widgets for Jupyter Notebook: a text input widget

Widgets for Jupyter notebook ¶ Let's import the module ipywidgets into the Jupyter Notebook from ipywidgets import widgets from ipywidgets import * from traitlets import * Now we import the display function from IPython ¶ let's attach a function to the event on_submit After we run this cell, we can go up and write something in the text widget and after you submit the text you wrote will be printed after the cell from IPython.display import display text = widgets . Text () display ( text ) def handle_submit ( sender ): print ( "Thank you for entering this text:" , text . value ) text . on_submit ( handle_submit ) Thank you for entering this text: Ciao

Image in Jupyter and PIL step by step

Hi, """ Hi, we will see a step by step tutorial about PIL and IPython.core.display modules to create images from other images and diplaying them in Jupyter notebook """ # What we will do # Create a card # 1. Take a pic of a heart # 2. Create an image blanck the size of a card 90*130 # 3. Paste the heart in the middle # 4. show the card """ As first step wi will simply display an image on the notebook. I will show two way to display the image with 'display' from IPhyton a. Using the open method of PIL.Image (named Img) b. Using the Image method from the IPython.core.display module """ # 1. Take the pic of a heart from IPython.core.display import Image , display from PIL import Image as Img heart = 'img/heart.png' display ( Image ( heart )) display ( Img . open ( heart )) # 2. Create an image blanck the size of a card 90*130 # 3. Paste the heart in the middle #...

Let's draw a circle with PIL in Python

Let's continue making our coding around PIL. Let's start with some basic drawing: a circle from PIL import Image , ImageDraw img = Image . new ( "RGB" ,( 60 , 60 ), 'white' ) dr = ImageDraw . Draw ( img ) dr . ellipse (( 0 , 0 , 60 , 60 ), 'yellow' ) img . show () this is the image produced *If you use jupyter notebook, just write img at the end to see the output.