What If i don't want Zope sorting for me.
Dear List, I have a dictionary in REQUEST. <dtml-call "REQUEST.set('my_dic',{'ar_id':'Article ID','ar_head':'Head','ar_body':'Body'})"> <dtml-var "my_dic.keys()"> Zope will sort for me. How to use the original order ? Thanks in advance Terry
Terry wrote:
I have a dictionary in REQUEST.
<dtml-call "REQUEST.set('my_dic',{'ar_id':'Article ID','ar_head':'Head','ar_body':'Body'})"> <dtml-var "my_dic.keys()">
Zope will sort for me. How to use the original order ?
1. Don't use DTML for that kindof code, it's nasty. Use a Script (Python) instead. the above would be: context.REQUEST.set('my_dict', { 'ar_id' :'Aticle ID', 'ar_head':'Head', 'ar_body':'Body'}) return REQUEST.my_dict.keys() As for the sorting, Zope sorts nothing. Python Dictionaries don't promise to order their keys so you may get them back in any random order. cheers, Chris PS: HTML to a non-HTML list is also a no-no. Stop usign Outlook Express before it kills you ;-)
Hi friend, I tried a Python Script ### Python Script context.REQUEST.set('my_dic',{ 'ar_id':'Article ID', 'ar_head':'Head', 'ar_sbhdtease':'Sub Head Tease', 'ar_sbhdbody':'Sub Head Body', 'ar_byline':'By line', 'ar_body':'Body'}) print context.REQUEST.my_dic.keys() return printed ### End of Python Script The script return ####### ['ar_sbhdtease', 'ar_sbhdbody', 'ar_body', 'ar_id', 'ar_head', 'ar_byline'] ####### It just forget the original order I just defined. Any idea ? Terry Chris Withers wrote:
Terry wrote:
I have a dictionary in REQUEST.
<dtml-call "REQUEST.set('my_dic',{'ar_id':'Article ID','ar_head':'Head','ar_body':'Body'})"> <dtml-var "my_dic.keys()">
Zope will sort for me. How to use the original order ?
1. Don't use DTML for that kindof code, it's nasty. Use a Script (Python) instead.
the above would be:
context.REQUEST.set('my_dict', { 'ar_id' :'Aticle ID', 'ar_head':'Head', 'ar_body':'Body'}) return REQUEST.my_dict.keys()
As for the sorting, Zope sorts nothing. Python Dictionaries don't promise to order their keys so you may get them back in any random order.
cheers,
Chris
PS: HTML to a non-HTML list is also a no-no. Stop usign Outlook Express before it kills you ;-)
Terry wrote:
['ar_sbhdtease', 'ar_sbhdbody', 'ar_body', 'ar_id', 'ar_head', 'ar_byline']
####### It just forget the original order I just defined. Any idea ?
Terry, you don't read to good, especially Harry's post ;-) python dictionaries have no notion of order in their keys. If you need the keys of a dictionary to stay ordered, then you need to keep them in a seperate list and use that for ordering. cheers, Chris
I don't know about the internals of how dictionaries are implemented, but I would guess that there is some degree of hashing in it. Dictionaries are orderless, they don't have any concept of an order in them. The keys are probably hashed and stored in a convenient order so that lookup times are a minimal as possible. They are stored in whatever order makes the dictionary work most efficiently. If you want to store some ordering information, I think you'll have to keep information on that somewhere else. You might want to use a sequence of dictionary keys, since sequences are ordered. Harry On Monday 25 February 2002 2:36 pm, Terry wrote:
Dear List,
I have a dictionary in REQUEST.
<dtml-call "REQUEST.set('my_dic',{'ar_id':'Article ID','ar_head':'Head','ar_body':'Body'})"> <dtml-var "my_dic.keys()">
Zope will sort for me. How to use the original order ?
Thanks in advance
Terry
participants (3)
-
Chris Withers -
Harry Wilkinson -
Terry