Constructing a Dictionary


numberDict = {1:'one', 2:'two', 3:'three', 4:'four'} letterDict = {'vowel':['a','e','i','o','u'],\               'consonant':['b','c','d','f']} numbers = (1,2,3,4,5,6,7,8,9,0) letters = ('a','b','c','d','e','f') punct = ('.', '!', '?') charSetDict = {numbers:[], letters:[], punct:[]}

Constructing a dictionary in Python is a simple matter of assigning a group of values with associated keys to a variable. Although the values can be any Python object, the keys must either be a number, string, or immutable tuple.

Simple dictionaries are made up of simple one-to-one, key-to-value relationships. However, you can construct very complex dictionaries that can have one-to-many and even many-to-many value relationships.

A one-to-many relationship can be accomplished by simply using list objects as the values in the dictionary.

The many-to-many relationship will take more thought and effort; however, this relationship can be accomplished by using tuples as the key objects and list objects as the value objects in the dictionary.

#Simple one to one dictionary numberDict = {1:'one', 2:'two', 3:'three', 4:'four'} #One to many dictionary letterDict = {'vowel':['a','e','i','o','u'],\               'consonant':['b','c','d','f']} #Many to many dictionary numbers = (1,2,3,4,5,6,7,8,9,0) letters = ('a','b','c','d','e','f') punct = ('.', '!', '?') charSetDict = {numbers:[], letters:[], punct:[]}


def_dict.py



Python Phrasebook(c) Essential Code and Commands
Python Phrasebook
ISBN: 0672329107
EAN: 2147483647
Year: N/A
Pages: 138
Authors: Brad Dayley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net