Reputation: 3543
I wanted to use a dictionary both with the keys and also like lists with indexing. but i found that assignment to an dictionary is not in order.
my python script is:
left=['E','Z','T','Y','F']
for lhs in left:
first[lhs]=set()
follow[lhs]=set()
print first
and i get the output as:
{'E': set([])}
{'Z': set([]), 'E': set([])}
{'Z': set([]), 'E': set([]), 'T': set([])}
{'Y': set([]), 'Z': set([]), 'E': set([]), 'T': set([])}
{'Y': set([]), 'Z': set([]), 'E': set([]), 'T': set([]), 'F': set([])}
one time it's inserted at end and another time it's at the beginning. this made me to think that i dont know the dictionary at all. where can i know the dictionary in depth. and how can iterate through a dictionary with keys as well as with indexes. for this i'm using now:
for lhs in left:
print first[lhs]
this some how helps with indexing. but is there any other method?
Upvotes: 1
Views: 330
Reputation: 531075
Use collections.OrderedDict
. It requires Python 2.7, but it remembers the order in which keys are added, rather than storing them in an arbitrary order based on the underlying hash algorithm.
Update: to be precise, the storage of the dict is unchanged, but the iteration is implemented with an additional data structure to provide a fixed order based on the original insertion order of the keys.
Upvotes: 6