[BangPypers] sort query

Sidharth Kuruvila sidharth.kuruvila at gmail.com
Sat Oct 24 11:59:57 CEST 2009


Hi,

 One more thing, the dict in python is a hash map so the keys won't be
ordered. If you want an ordered set of keys you could consider storing
the keys in a separate ordered list. You could also consider building
a binary tree based dict but that would be a pain.

Also a quick search brought up this http://www.python.org/dev/peps/pep-0372/.

Regards,
Sidharth

On Sat, Oct 24, 2009 at 3:23 PM, Sidharth Kuruvila
<sidharth.kuruvila at gmail.com> wrote:
> Hi,
>
>>>> d = {"a":1, "b":2}
>>>> d.keys()
> ['a', 'b']
>>>> a = d.keys()
>>>> b = d.keys()
>>>> id(a)
> 542120
>>>> id(b)
> 542200
>
> So d creates a new list with each call to keys.
>
> The behavior might be different in python 3 where I hear d.keys() will
> return a set
>
> Regards,
> Sidharth
>
> On Sat, Oct 24, 2009 at 2:12 PM, Noufal Ibrahim <noufal at gmail.com> wrote:
>> On Sat, Oct 24, 2009 at 1:32 PM, bhaskar jain
>> <bhaskar.jain2002 at gmail.com> wrote:
>>> Hello,
>>>
>>>  Can sort not modify read-only location.
>>>
>>>>>> d
>>> {'a': 1, 'c': 3, 'b': 2}
>>>
>>>>>> id(d)
>>> 412816
>>>
>>>>>> id(d.keys())
>>> 404296
>>>
>>>>>> type(d.keys())
>>> <type 'list'>
>>>
>>>>>> print d.keys().sort()
>>
>> The sort method of a list doesn't return a sorted list. As for the
>> question of what exactly did that sort and where is the sorted list,
>> I'm not sure.
>>
>>
>> --
>> ~noufal
>> http://nibrahim.net.in
>> _______________________________________________
>> BangPypers mailing list
>> BangPypers at python.org
>> http://mail.python.org/mailman/listinfo/bangpypers
>>
>
>
>
> --
> I am but a man.
>



-- 
I am but a man.


More information about the BangPypers mailing list