Re: [BangPypers] Dictionary : An elementary question
On Thu, Aug 19, 2010 at 10:31 AM, Sahasranaman MS sah...@naman.ms wrote: On Thursday 19 August 2010 07:08 AM, Kenneth Gonsalves wrote: On Wed, 2010-08-18 at 10:28 -0700, Anand Shankar wrote: I have no clues. Any inputs?? sort order of dictionary keys is not guaranteed. Only a list will return items in the same order as entered. Python has an OrderedDict class from Python 3.1 http://docs.python.org/dev/whatsnew/3.1.html#pep-372-ordered-dictionaries For older versions of python, there are several other implementations that you could use. OrderedDict = BST Dict = HashTable ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers -- ~l0nwlf ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Dictionary : An elementary question
On Thu, Aug 19, 2010 at 10:31 AM, Sahasranaman MS sah...@naman.ms wrote: On Thursday 19 August 2010 07:08 AM, Kenneth Gonsalves wrote: On Wed, 2010-08-18 at 10:28 -0700, Anand Shankar wrote: I have no clues. Any inputs?? sort order of dictionary keys is not guaranteed. Only a list will return items in the same order as entered. Python has an OrderedDict class from Python 3.1 http://docs.python.org/dev/whatsnew/3.1.html#pep-372-ordered-dictionaries For older versions of python, there are several other implementations that you could use. When I was writing my reply I was thinking - wasnt there something added recently which is a dictionary and was ordered. Unfortunately I'm stuck on 2.5, so havent used this one. Thanks for this. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Dictionary : An elementary question
Thanks a Ton to all. I got a mucher deeper insight than my question deserved!!! Thanks once again anand ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Dictionary : An elementary question
Hi All, There really shouldn't be so much debate on the question asked. Someone actually gave a direct and clear answer. I'm new at python and his explanations were quite understandable. As far as dictionaries are concerned, when you retrieve keys, there is no guarantee of a particular order. If you call the d.keys() consecutively, you might get two different orders. However, to get it in an alphabetical order, there is a sort() method to use. I am not too sure of the syntax but it should be something like this: sort(d.keys()). Please if I'm wrong point it out immediately to avoid transmitting wrong codes. I hope it helps. Regards, On 8/19/10, Shashwat Anand anand.shash...@gmail.com wrote: On Wed, Aug 18, 2010 at 10:58 PM, Anand Shankar anand_shan...@yahoo.comwrote: During a tutorial python session with my colleagues I was presented with a basic question d = {'apple':2,'banana':5, 'coke': 6} print d.keys() ['coke', 'apple', 'banana'] Question is why does it not return ['apple','banana','coke'] Similarly: d = {'a':2,'b':4,'c':5,'d':4,'e':3} print d.keys() ['a', 'c', 'b', 'e', 'd'] why not ['a', 'b', 'c', 'd', 'e'] I have no clues. Any inputs?? You will most likely want to read about how Hash-tables work. anand ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers -- ~l0nwlf ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers -- Sent from my mobile device Elegbede Muhammed Oladipupo OCA +2348077682428 +2347042171716 www.dudupay.com Mobile Banking Solutions | Transaction Processing | Enterprise Application Development ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Dictionary : An elementary question
Here is a small script to sort the dictionary based on key/choice a = {key3: 5 , key2: 8, key1: 2} b = {key2: 7 , key1: 4, key3: 9} c = {key1: 6 , key3: 1, key2: 1} undecorated = [a, b, c] # how do you sort this list? sort_on = key3 decorated = [(dict_[sort_on], dict_) for dict_ in [b]] decorated.sort() print decorated print --- result = [dict_ for (key, dict_) in decorated] print result Praveen Kumar +91 9620621342 http://praveensunsetpoint.wordpress.com Bangalore On Thu, Aug 19, 2010 at 10:26 AM, Dipo Elegbede delegb...@dudupay.comwrote: Hi All, There really shouldn't be so much debate on the question asked. Someone actually gave a direct and clear answer. I'm new at python and his explanations were quite understandable. As far as dictionaries are concerned, when you retrieve keys, there is no guarantee of a particular order. If you call the d.keys() consecutively, you might get two different orders. However, to get it in an alphabetical order, there is a sort() method to use. I am not too sure of the syntax but it should be something like this: sort(d.keys()). Please if I'm wrong point it out immediately to avoid transmitting wrong codes. I hope it helps. Regards, On 8/19/10, Shashwat Anand anand.shash...@gmail.com wrote: On Wed, Aug 18, 2010 at 10:58 PM, Anand Shankar anand_shan...@yahoo.comwrote: During a tutorial python session with my colleagues I was presented with a basic question d = {'apple':2,'banana':5, 'coke': 6} print d.keys() ['coke', 'apple', 'banana'] Question is why does it not return ['apple','banana','coke'] Similarly: d = {'a':2,'b':4,'c':5,'d':4,'e':3} print d.keys() ['a', 'c', 'b', 'e', 'd'] why not ['a', 'b', 'c', 'd', 'e'] I have no clues. Any inputs?? You will most likely want to read about how Hash-tables work. anand ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers -- ~l0nwlf ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers -- Sent from my mobile device Elegbede Muhammed Oladipupo OCA +2348077682428 +2347042171716 www.dudupay.com Mobile Banking Solutions | Transaction Processing | Enterprise Application Development ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Dictionary : An elementary question
On Thu, Aug 19, 2010 at 10:26 AM, Dipo Elegbede delegb...@dudupay.comwrote: Hi All, There really shouldn't be so much debate on the question asked. Someone actually gave a direct and clear answer. I'm new at python and his explanations were quite understandable. I'm sorry that you sensed a debate in what happened to the question. Personally I find it very refreshing when people contribute with different ideas. In this particular case, Shaswat Anand's reply of what OrdererdDicts are (BSTs) was actually a new information to me. Had we closed the question the moment we sense a right answer, I wouldnt have known that. If you call the d.keys() consecutively, you might get two different orders. I dont think it works the way you say it. d.keys() *should* return the same way no matter how many times you call it. If the dictionary (d) changes in between - because of the way HashTables/HashMaps are implemented - you are bound to get in a different order. *Long story short. *d.keys() will return the same thing if d doesnt change. /jeff ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Dictionary : An elementary question
Lots of good answers. Warning: *this answer is ultra simplistic one to explain the implementation succinctly*. A little more from an implementation perspective dict operations usually involve converting a dict into a hash. This hash is then converted into a bucket. And sometimes when one gets into a collision, an alternative bucket may need to get selected. In a simplistic example lets say we have 100 buckets, the hash is computed by adding the ascii values of the bytes comprising the key. Now lets assume the the key is the character 'AB' Since the ascii code of 'A' is 65, and 'B' is 66 and there are no other characters, then given our hash algorithm the hash will work out to 65 + 66 = 131 A simple hash to bucket mapping model is to use mod / modulo. Since 131 mod 100 which leads us to 31. Assuming there are no key collisions and the buckets are stored as an array, the key will now be stored in bucket no. 31 (zero based index). If there is a collision - the key will be stored in some other bucket based on the resolution provided by the collision management algorithm Usually when one invokes a .keys() method, the most efficient way is to run through the buckets array and spit out the keys stored in each non empty bucket. That should hopefully clarify why .keys() generally does not return keys ordered by the keys themselves, it often will return keys ordered by the buckets they are stored into. Also since changing the internal logic of the hash table with regards to key - hash - bucket mapping and collision management, will change the internal order for the same set of keys. Similarly even if the same keys are inserted into the hash table in different orders, the resultant order of keys can change if some of the keys collide. As a result hash tables will generally not issue any guarantee any ordering on the keys since that would require additional efforts and the implied performance loss - something thats often unnecessary for most typical dict operations. Dhananjay On Wed, Aug 18, 2010 at 10:58 PM, Anand Shankar anand_shan...@yahoo.comwrote: During a tutorial python session with my colleagues I was presented with a basic question d = {'apple':2,'banana':5, 'coke': 6} print d.keys() ['coke', 'apple', 'banana'] Question is why does it not return ['apple','banana','coke'] Similarly: d = {'a':2,'b':4,'c':5,'d':4,'e':3} print d.keys() ['a', 'c', 'b', 'e', 'd'] why not ['a', 'b', 'c', 'd', 'e'] I have no clues. Any inputs?? anand ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers -- blog: http://blog.dhananjaynene.com twitter: http://twitter.com/dnene ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Dictionary : An elementary question
On Wed, Aug 18, 2010 at 10:58 PM, Anand Shankar anand_shan...@yahoo.com wrote: During a tutorial python session with my colleagues I was presented with a basic question d = {'apple':2,'banana':5, 'coke': 6} print d.keys() ['coke', 'apple', 'banana'] Question is why does it not return ['apple','banana','coke'] Similarly: d = {'a':2,'b':4,'c':5,'d':4,'e':3} print d.keys() ['a', 'c', 'b', 'e', 'd'] why not ['a', 'b', 'c', 'd', 'e'] I have no clues. Any inputs?? Quoting from python docs[1] The keys() method of a dictionary object returns a list of all the keys used in the dictionary, in arbitrary order (if you want it sorted, just apply the sort() method to the list of keys). [1]: http://docs.python.org/tutorial/datastructures.html#dictionaries regards Vivek -- The hidden harmony is better than the obvious!! ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Dictionary : An elementary question
On 18 August 2010 22:58, Anand Shankar anand_shan...@yahoo.com wrote: During a tutorial python session with my colleagues I was presented with a basic question d = {'apple':2,'banana':5, 'coke': 6} print d.keys() ['coke', 'apple', 'banana'] Question is why does it not return ['apple','banana','coke'] snip I have no clues. Any inputs?? For this reason: It is best to think of a dictionary as an unordered set of key: value pairs http://docs.python.org/tutorial/datastructures.html#dictionaries d = {'apple':2,'banana':5, 'coke': 6} d {'coke': 6, 'apple': 2, 'banana': 5} print d.keys() ['coke', 'apple', 'banana'] -- Vinay S Shastry http://thenub.one09.net ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Dictionary : An elementary question
Excellent responses so far. Dictionaries are optimized for retrieving key/value pairs. And to achieve that, it compromises on the order in which stuff is stored. This and more is very nicely presented in the Pycon 2010 talk - The Mighty Dictionary. Highly recommended. http://us.pycon.org/2010/conference/schedule/event/12/ http://us.pycon.org/2010/conference/schedule/event/12//jeff On Wed, Aug 18, 2010 at 11:04 PM, Vinay Shastry vinayshas...@gmail.comwrote: On 18 August 2010 22:58, Anand Shankar anand_shan...@yahoo.com wrote: During a tutorial python session with my colleagues I was presented with a basic question d = {'apple':2,'banana':5, 'coke': 6} print d.keys() ['coke', 'apple', 'banana'] Question is why does it not return ['apple','banana','coke'] snip I have no clues. Any inputs?? For this reason: It is best to think of a dictionary as an unordered set of key: value pairs http://docs.python.org/tutorial/datastructures.html#dictionaries d = {'apple':2,'banana':5, 'coke': 6} d {'coke': 6, 'apple': 2, 'banana': 5} print d.keys() ['coke', 'apple', 'banana'] -- Vinay S Shastry http://thenub.one09.net ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Dictionary : An elementary question
On Wed, Aug 18, 2010 at 10:58 PM, Anand Shankar anand_shan...@yahoo.comwrote: During a tutorial python session with my colleagues I was presented with a basic question d = {'apple':2,'banana':5, 'coke': 6} print d.keys() ['coke', 'apple', 'banana'] Question is why does it not return ['apple','banana','coke'] Similarly: d = {'a':2,'b':4,'c':5,'d':4,'e':3} print d.keys() ['a', 'c', 'b', 'e', 'd'] why not ['a', 'b', 'c', 'd', 'e'] I have no clues. Any inputs?? You will most likely want to read about how Hash-tables work. anand ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers -- ~l0nwlf ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Dictionary : An elementary question
On Thursday 19 August 2010 07:08 AM, Kenneth Gonsalves wrote: On Wed, 2010-08-18 at 10:28 -0700, Anand Shankar wrote: I have no clues. Any inputs?? sort order of dictionary keys is not guaranteed. Only a list will return items in the same order as entered. Python has an OrderedDict class from Python 3.1 http://docs.python.org/dev/whatsnew/3.1.html#pep-372-ordered-dictionaries For older versions of python, there are several other implementations that you could use. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers