1 / 7

Dictionaries

Dictionaries. Emad Nawfal. A minimal dictionary. english2arabic = {'man': 'rajul', 'country': 'balad', 'peace': 'salam', 'terror': 'irhab', 'child': 'tifl'}. Dictionaries have pairs of items.

Download Presentation

Dictionaries

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Dictionaries Emad Nawfal

  2. A minimal dictionary english2arabic = {'man': 'rajul', 'country': 'balad', 'peace': 'salam', 'terror': 'irhab', 'child': 'tifl'}

  3. Dictionaries have pairs of items. • Each item has a key and a value. • Dictionaries are indexed by keys, not by digits indicating position as in lists • The key has to be an immutable type: what are immutable types? • The values can be anything.

  4. Dictionaries support the in operator (tests for keys, not values) • Dictionaries support iteration • The .values() method gets us all the values. • The .keys() method gets us all the keys. • The .items() method gets us all the items. • Dict1.py as an example

  5. Building dictionaries programmatically • It is very tedious to build dictionaries by hand. • You can build dictionaries programmatically easily in two steps: • Initialize a dictionary • Update the dictionary keys.

  6. Counting words with dictionaries • Question: How do you count word frequencies in a text? • Can we use: listname.count(x)? • Dictionaries: buildDict.py • Default dictionaries: countWords.py

  7. Advanced Topics • Dictionaries of dictionaries • Dictionaries of lists • Lists of dictionaries • Use a database if possible. Things get ugly quickly.

More Related