944,162 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 49102
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 8th, 2005
2

Ways to create Dictionary

Expand Post »
There seems to be many ways to create a dictionary, which one do you prefer?
Similar Threads
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Oct 9th, 2005
0

Re: Ways to create Dictionary

For those who want to minimize typing ...
Python Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 9th, 2005
0

Re: Ways to create Dictionary

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 ...
python Syntax (Toggle Plain Text)
  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 ...
python Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 12th, 2005
0

Re: Ways to create Dictionary

... 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)
  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
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 12th, 2005
0

Re: Ways to create Dictionary

I think an empty dictionary can be created with:
Python Syntax (Toggle Plain Text)
  1. emptyD1 = {}
  2. emptyD2 = dict()
  3. print emptD1, emptyD2
Then is the normal perhaps called direct way:
Python Syntax (Toggle Plain Text)
  1. dict1 = {'a':1, 'b':2, 'c':3, 'd':4} # etc.
  2. print dic1
Does this way have a name?
Python Syntax (Toggle Plain Text)
  1. dic2 = {}
  2. dic2['Linux'] = 'software'
  3. dic2['floppy'] = 'accessory'
  4. dic2['floppy drive'] = 'hardware'
  5. dic2['CPU'] = 'hardware' # etc.
  6. print dic2
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Oct 12th, 2005
-1

Re: Ways to create Dictionary

Your last way is officially called "using indexing and assignment".
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Apr 19th, 2010
0

i need more explanation

Click to Expand / Collapse  Quote originally posted by bumsfeld ...
There seems to be many ways to create a dictionary, which one do you prefer?
i'm a student and i want to create an electronic dictionary, can you help me?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ajipw is offline Offline
1 posts
since Apr 2010
Apr 19th, 2010
0
Re: Ways to create Dictionary
Click to Expand / Collapse  Quote originally posted by ajipw ...
i'm a student and i want to create an electronic dictionary, can you help me?
You should start your own thread instead of reviving an old one, and also tell us what you mean by 'create an electronic dictionary', which is not exactly the same thing as a python dictionary.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,656 posts
since Jul 2008
Feb 9th, 2011
0
Re: Ways to create Dictionary
wow wegaseat!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
babyash is offline Offline
1 posts
since Feb 2011
Feb 10th, 2011
0
Re: Ways to create Dictionary
http://docs.python.org/library/stdty...l#typesmapping

Shows you the most common ways.


zzzzz... lol thread necro.
Last edited by Enders_Game; Feb 10th, 2011 at 9:00 am.
Reputation Points: 23
Solved Threads: 6
Newbie Poster
Enders_Game is offline Offline
23 posts
since Mar 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Pydev problem
Next Thread in Python Forum Timeline: help with python syntax in cypher program?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC