| | |
Ways to create Dictionary
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
For those who want to minimize typing ...
Python Syntax (Toggle Plain Text)
# this saves you from having to add quotes around any strings/characters (keys only) dic1 = dict(a=1, b=2, c=3, d=4) print dic1 # {'a': 1, 'c': 3, 'b': 2, 'd': 4}
Last edited by vegaseat; Aug 12th, 2009 at 5:25 pm.
May 'the Google' be with you!
Let's say you want to create an English to French dictionary. You go to babelfish.altavista.com and enter "one, two, three, four, five, six, seven, eight, nine, ten" tell it to translate that from English to French and it gives you "un, deux, trois, quatre, cinq, six, sept, huit, neuf, dix". The easy way to make a dictionary from this after copy and paste ...
The same thing even shorter ...
python Syntax (Toggle Plain Text)
str1 = "one, two, three, four, five, six, seven, eight, nine, ten" # after babelfish.altavista.com English to French translation str2 = "un, deux, trois, quatre, cinq, six, sept, huit, neuf, dix" engList = str1.split(", ") frenList = str2.split(", ") eng2frenDict = {} k = 0 for eng in engList: eng2frenDict[eng] = frenList[k] k += 1 print eng2frenDict """ result = {'seven': 'sept', 'ten': 'dix', 'nine': 'neuf', 'six': 'six', 'three': 'trois', 'two': 'deux', 'four': 'quatre', 'five': 'cinq', 'eight': 'huit', 'one': 'un'} """
python Syntax (Toggle Plain Text)
str1 = "one, two, three, four, five, six, seven, eight, nine, ten" str2 = "un, deux, trois, quatre, cinq, six, sept, huit, neuf, dix" engList = str1.split(", ") frenList = str2.split(", ") eng2frenDict = dict(zip(engList, frenList)) print eng2frenDict
Last edited by vegaseat; Aug 12th, 2009 at 5:25 pm.
May 'the Google' be with you!
... just one more way to create a dictionary, this one does a character count in a text string and gives you a dictionary of char:count pairs ...
python Syntax (Toggle Plain Text)
# count the characters in a string and create a dictionary of char:count pairs str1 = 'supercalifragilisticexpialidocious' print "Count the characters in: %s" % str1 charCount = {} for char in str1: charCount[char] = charCount.get(char, 0) + 1 print charCount # {'a': 3, 'c': 3, 'e': 2, 'd': 1, 'g': 1, ... }
Last edited by vegaseat; Aug 12th, 2009 at 5:26 pm. Reason: changed the darn php code tags
May 'the Google' be with you!
I think an empty dictionary can be created with:
Then is the normal perhaps called direct way:
Does this way have a name?
Python Syntax (Toggle Plain Text)
emptyD1 = {} emptyD2 = dict() print emptD1, emptyD2
Python Syntax (Toggle Plain Text)
dict1 = {'a':1, 'b':2, 'c':3, 'd':4} # etc. print dic1
Python Syntax (Toggle Plain Text)
dic2 = {} dic2['Linux'] = 'software' dic2['floppy'] = 'accessory' dic2['floppy drive'] = 'hardware' dic2['CPU'] = 'hardware' # etc. print dic2
![]() |
Similar Threads
- How to map one list onto another using a dictionary?? (Python)
- Dictionary Keys (Python)
- Merging Dictionary (Python)
- Dynamic web pages? Which will exist? (IT Professionals' Lounge)
Other Threads in the Python Forum
- Previous Thread: Python and the JPEG Image File, Part 2, The Image
- Next Thread: jpg with Python
Views: 17054 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Python
anti array avogadro beginner builtin clear client code color count csv curved def dictionary dynamic enter examples excel file float format frange ftp function gui heads hints homework import input java lapse line lines linux list lists loop microcontroller mouse multiple mysqldb mysqlquery newb number numbers output parsing path port prime program programming projects py2exe pygame pyopengl pyqt python random raw_input recursion recursive redirect script scrolledtext singleton software sqlite ssh stderr string strings subprocess sum syntax table terminal text thread threading time tkinter tlapse tooltip tuple tutorial twoup ubuntu unicode unix urllib urllib2 variable web-scrape wikipedia windows word wx.wizard wxpython






