Ways to create Dictionary

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 138
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Ways to create Dictionary

 
0
  #1
Oct 8th, 2005
There seems to be many ways to create a dictionary, which one do you prefer?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,141
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 947
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Ways to create Dictionary

 
0
  #2
Oct 9th, 2005
For those who want to minimize typing ...
  1. # this saves you from having to add quotes around any strings/characters (keys only)
  2. dic1 = dict(a=1, b=2, c=3, d=4)
  3.  
  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!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,141
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 947
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Ways to create Dictionary

 
0
  #3
Oct 9th, 2005
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 ...
  1. str1 = "one, two, three, four, five, six, seven, eight, nine, ten"
  2. # after babelfish.altavista.com English to French translation
  3. str2 = "un, deux, trois, quatre, cinq, six, sept, huit, neuf, dix"
  4.  
  5. engList = str1.split(", ")
  6. frenList = str2.split(", ")
  7. eng2frenDict = {}
  8. k = 0
  9. for eng in engList:
  10. eng2frenDict[eng] = frenList[k]
  11. k += 1
  12.  
  13. print eng2frenDict
  14. """ result =
  15. {'seven': 'sept', 'ten': 'dix', 'nine': 'neuf', 'six': 'six', 'three': 'trois',
  16. 'two': 'deux', 'four': 'quatre', 'five': 'cinq', 'eight': 'huit', 'one': 'un'}
  17. """
The same thing even shorter ...
  1. str1 = "one, two, three, four, five, six, seven, eight, nine, ten"
  2. str2 = "un, deux, trois, quatre, cinq, six, sept, huit, neuf, dix"
  3.  
  4. engList = str1.split(", ")
  5. frenList = str2.split(", ")
  6. eng2frenDict = dict(zip(engList, frenList))
  7. print eng2frenDict
Last edited by vegaseat; Aug 12th, 2009 at 5:25 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,141
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 947
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Ways to create Dictionary

 
0
  #4
Oct 12th, 2005
... 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 ...
  1. # count the characters in a string and create a dictionary of char:count pairs
  2.  
  3. str1 = 'supercalifragilisticexpialidocious'
  4. print "Count the characters in: %s" % str1
  5. charCount = {}
  6. for char in str1:
  7. charCount[char] = charCount.get(char, 0) + 1
  8.  
  9. 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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 138
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Ways to create Dictionary

 
0
  #5
Oct 12th, 2005
I think an empty dictionary can be created with:
  1. emptyD1 = {}
  2. emptyD2 = dict()
  3. print emptD1, emptyD2
Then is the normal perhaps called direct way:
  1. dict1 = {'a':1, 'b':2, 'c':3, 'd':4} # etc.
  2. print dic1
Does this way have a name?
  1. dic2 = {}
  2. dic2['Linux'] = 'software'
  3. dic2['floppy'] = 'accessory'
  4. dic2['floppy drive'] = 'hardware'
  5. dic2['CPU'] = 'hardware' # etc.
  6. print dic2
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,141
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 947
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Ways to create Dictionary

 
0
  #6
Oct 12th, 2005
Your last way is officially called "using indexing and assignment".
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum


Views: 17054 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC