ling 555 programming for linguists
play

Ling 555 Programming for Linguists Python Dictionaries Robert - PowerPoint PPT Presentation

Ling 555 Programming for Linguists Python Dictionaries Robert Albert Felty Speech Research Laboratory Indiana University Sep. 28, 2008 L555 Escaping string Sep. 28 escaped="Before adding text to SQL Homework questions


  1. Ling 555 — Programming for Linguists Python — Dictionaries Robert Albert Felty Speech Research Laboratory Indiana University Sep. 28, 2008

  2. L555 Escaping string Sep. 28 escaped="Before adding text to SQL Homework questions databases , all single \\ ' \\ ' and hmwk2 double quote \\\"\\\" characters (as well as the backslash Miscellaneous tips character \\\\ itself) must be escaped with the backslash Dictionary \\\"\\\\\\\" character." basics print escaped Dictionary #OR methods escaped=r"Before adding text to SQL databases , all single \ ' \ ' and double quote \"\" characters (as well as the backslash character \\ itself) must be escaped with the backslash \"\\\" character." 3

  3. L555 Sorting lists and tuples Sep. 28 Homework Tip: questions You can sort a list of tuples. The sorting will be done Miscellaneous first by the first element of the tuple, and then by the tips second element sorting importing Example Dictionary lot=[(1,2), (2,1), (4,3), (2,2), basics (3,2), (1,1), ("3","a")] Dictionary lot.sort() methods # OR, to create a new list lot_sorted=sorted(lot) More on sorting at: http://wiki.python.org/moin/HowTo/Sorting 4

  4. L555 Mixing interactive and non-interactive Sep. 28 Homework Tip questions You can mix an interactive and non-interactive session. Miscellaneous tips Script File: sorting #!/usr/bin/env python importing gradebook=dict() Dictionary basics Interactive: Dictionary methods from hmwk4.py import * type(gradebook) 5

  5. L555 String formatting Sep. 28 Homework Definition questions You can use dictionary keys in string formatting. Miscellaneous tips practice Dictionary Create a new dictionary called phonebook, and store basics your phone number in it. string Print out your name and number using the 1 dol,lod, etc. dictionary style syntax Dictionary phonebook=dict() methods phonebook[ ' rob ' ]= ' 555-6785 ' print( ' Rob: %(rob)s ' % phonebook) 6

  6. L555 X of X Sep. 28 Homework Dictionaries of lists et al. questions We already knew about lists of lists. Now we can also Miscellaneous do: tips Lists of dictionaries lod[4][’number’] Dictionary Dictionaries of dictionaries basics dod[’four’][’number’] string Dictionaries of lists lod[’four’][4] dol,lod, etc. And any other combination, e.g. lolod, lodod, dodol, Dictionary methods dododolod 7

  7. L555 X of X Sep. 28 Homework Practice questions Construct a class database. For each person, include 1 Miscellaneous their nickname and favorite color. tips theclass=dict() Dictionary theclass[ ' robfelty ' ]= { ' nickname ' : basics ' forest green ' } ' rob ' , ' color ' : string theclass[ ' mabdulma ' ]= { ' nickname ' : dol,lod, etc. ' orange ' } ' Muhammad ' , ' color ' : Dictionary theclass[ ' djefford ' ]= { ' nickname ' : methods ' red ' } ' Dustin ' , ' color ' : Print out the favorite color for one person 2 print(theclass[ ' robfelty ' ][ ' color ' ]) 8

  8. L555 keys, values Sep. 28 Homework practice questions Print the keys from the class database 1 Miscellaneous print(theclass.keys()) tips Sort the keys from the class database 2 Dictionary theclasskeys=theclass.keys() basics theclasskeys.sort() Dictionary Print just the values from the class database 3 methods print(theclass.values()) keys items other methods 9

  9. L555 items Sep. 28 Homework practice questions Convert the dictionary into a list 1 Miscellaneous theclasslist=theclass.items() tips Print out the class database, sorted by username. 2 Dictionary theclasslist.sort() basics print(theclasslist) Dictionary methods keys items other methods 10

  10. L555 other methods Sep. 28 Homework questions? questions Any questions on other dictionary methods the book Miscellaneous mentions? tips Dictionary basics Dictionary methods keys items other methods 11

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend