Skip to main content

Ordering lists, splitting strings and other stuffs...

Let's sort a list of names... in many ways

Hi, let's say we have a list of names like the following:

listofnames = ['Caputo Mariangela', 'Filomena Cortazzo', 'Tommaso Peduto', 'Filomena Rossi', 'Antonietta Russo', 'Caputo Iuliano', 'Rosalba Rubano', 'Marotta Rubano', 'Daniela Cortazzo', 'Anna Gallo', 'Nunzio Buccino', 'Maurizio Amoresano', 'Rosalba Concetta', 'Maria Isabella Luongo', 'Iuliano Mariangela', 'Derna Cimmino', 'Anna Rossi', 'Flavia Damato', 'Derna La Marca', 'Guglielmo Mariangela', 'Caruccio Mariangela', 'Regina Madaio', 'Vincenza Balzano', 'Maurizio Giorno', 'Marco Lenza', 'Domenico Tancredi', 'Ortensia Russo', 'Carlo Amoresano', 'Guglielmo De Luca', 'Annapina Califano', 'Gaetana Margiotta', 'Tommaso La Regina', 'Nevia Scarano', 'Antonietta Turriziani', 'Guglielmo Sansone', 'Antonietta Guariglia', 'Antonio Mariagrazia', 'Anella De Luca', 'Giovanni Luisa Benformato', 'Domenico Velella', 'Daniela Luongo', 'Vincenza Concetta', 'Carla Grasso', 'Angelamaria Manzo', 'Pierpaolo Passaro', 'Annalisa Starace', 'Maria Isabella Madaio', 'Pasqualina Guercio', 'Marco La Marca', 'Antonietta Passaro']

What if we want to sort them?
It's easy

>>> listofnames.sort()
['Anella De Luca', 'Angelamaria Manzo', 'Anna Gallo', 'Anna Rossi', 'Annalisa Starace', 'Annapina Califano', 'Antonietta Guariglia', 'Antonietta Passaro', 'Antonietta Russo', 'Antonietta Turriziani', 'Antonio Mariagrazia', 'Caputo Iuliano', 'Caputo Mariangela', 'Carla Grasso', 'Carlo Amoresano', 'Caruccio Mariangela', 'Daniela Cortazzo', 'Daniela Luongo', 'Derna Cimmino', 'Derna La Marca', 'Domenico Tancredi', 'Domenico Velella', 'Filomena Cortazzo', 'Filomena Rossi', 'Flavia Damato', 'Gaetana Margiotta', 'Giovanni Luisa Benformato', 'Guglielmo De Luca', 'Guglielmo Mariangela', 'Guglielmo Sansone', 'Iuliano Mariangela', 'Marco La Marca', 'Marco Lenza', 'Maria Isabella Luongo', 'Maria Isabella Madaio', 'Marotta Rubano', 'Maurizio Amoresano', 'Maurizio Giorno', 'Nevia Scarano', 'Nunzio Buccino', 'Ortensia Russo', 'Pasqualina Guercio', 'Pierpaolo Passaro', 'Regina Madaio', 'Rosalba Concetta', 'Rosalba Rubano', 'Tommaso La Regina', 'Tommaso Peduto', 'Vincenza Balzano', 'Vincenza Concetta']


But, what if we want to order by surname.

we should:

1) Cycle trought the list (for...)
2) Split the name in 2 3 4 parts (some names are made of three parts)
3) if there are 3 parts and the second part is "De" or "La" the second and third part si the surname
4) otherwise the first and the second are first and second name

------------------------- code ----------------------------------

wholename = [] # we will store here the surname + name
for names in listofnames:
    splitted = names.split() # let's split the name
    if len(splitted) == 3: # 2 names or a surname in 2 parts
        if splitted[1] == "De" or splitted[1]== "La": 
        # it checks for De or La: i.e. a surname in 2 parts
        # So I append to another list 
        # the first part of the surname + second part + name
            wholename.append(splitted[1] + " " + splitted[2] + " " + splitted[0])
        else:
            # If the second part is not De or La ...
            # put last part of the 3 part at the start
            # PS: remember that lists starts from 0
            wholename.append(splitted[2] + " " + splitted[0] + " " + splitted[1])
    elif len(splitted) == 4: # 2 names and 2 surnames or 3 names
        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])
        # at last, the simpliest case: 1 name and 1 surname...
    else:
   wholename.append(splitted[1] + " " +  splitted[0])

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


This is the output:
-------------------------


0 ) Amoresano Carlo
1 ) Amoresano Maurizio
2 ) Balzano Vincenza
3 ) Benformato Giovanni Luisa
4 ) Buccino Nunzio
5 ) Califano Annapina
6 ) Cimmino Derna
7 ) Concetta Rosalba
8 ) Concetta Vincenza
9 ) Cortazzo Daniela
10 ) Cortazzo Filomena
11 ) Damato Flavia
12 ) De Luca Anella
13 ) De Luca Guglielmo
14 ) Gallo Anna
15 ) Giorno Maurizio
16 ) Grasso Carla
17 ) Guariglia Antonietta
18 ) Guercio Pasqualina
19 ) Iuliano Caputo
20 ) La Marca Derna
21 ) La Marca Marco
22 ) La Regina Tommaso
23 ) Lenza Marco
24 ) Luongo Daniela
25 ) Luongo Maria Isabella
26 ) Madaio Maria Isabella
27 ) Madaio Regina
28 ) Manzo Angelamaria
29 ) Margiotta Gaetana
30 ) Mariagrazia Antonio
31 ) Mariangela Caputo
32 ) Mariangela Caruccio
33 ) Mariangela Guglielmo
34 ) Mariangela Iuliano
35 ) Passaro Antonietta
36 ) Passaro Pierpaolo
37 ) Peduto Tommaso
38 ) Rossi Anna
39 ) Rossi Filomena
40 ) Rubano Marotta
41 ) Rubano Rosalba
42 ) Russo Antonietta
43 ) Russo Ortensia
44 ) Sansone Guglielmo
45 ) Scarano Nevia
46 ) Starace Annalisa
47 ) Tancredi Domenico
48 ) Turriziani Antonietta
49 ) Velella Domenico

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.