SmtC: Show me the Code
Ole Peter Smith
Instituto de Matemática e Estatística
Universidade Federal de Goiás
http://www.olesmith.com.br

Dictionaries
Problemas resolvemos na hora!
Milagres demoram mais um pouco...
Provérbio dinamarquês
< Lists | List of Dictionaries | XML >

List of Dictionaries

??First.py3 Listing: First.py3.
#!/usr/bin/python3

#A dict
person={
    "Name": "Ole",
    "Email": "ole@ufg.br",
    "Birthday": "11/01/1964",
    "Phone": "62 5555555",
    "Fax": "62 5555555",
}

persons=list()
for i in range(50):
    rperson=dict(person)
    rperson[ "No" ]=i
    persons.append(rperson)

#Slicing
persons=persons[10:15]

i=0
for person in persons:
    i+=1
    keys=list(person.keys())
    keys.sort()
    print(i)
    
    for key in keys:
        print(key+":",person[key])

    print()
        
#Tarefa: Criar uma lista (list) de 10 pessoas (dict) diferentes
#Produzir um tabela com colunas Name, Email e Phone

Output:
1
Birthday: 11/01/1964
Email: ole@ufg.br
Fax: 62 5555555
Name: Ole
No: 10
Phone: 62 5555555

2
Birthday: 11/01/1964
Email: ole@ufg.br
Fax: 62 5555555
Name: Ole
No: 11
Phone: 62 5555555

3
Birthday: 11/01/1964
Email: ole@ufg.br
Fax: 62 5555555
Name: Ole
No: 12
Phone: 62 5555555

4
Birthday: 11/01/1964
Email: ole@ufg.br
Fax: 62 5555555
Name: Ole
No: 13
Phone: 62 5555555

5
Birthday: 11/01/1964
Email: ole@ufg.br
Fax: 62 5555555
Name: Ole
No: 14
Phone: 62 5555555


Output from: /usr/bin/python3 First.py3
< Lists | List of Dictionaries | XML >
Messages:
0 secs.