Alphabetized dictionary listings
Emile van Sebille
emile at fenx.com
Thu Nov 25 11:01:16 EST 1999
Ken,
It sounds like you're looking for something like this.
#-------Start Code Fragment--------
def display(d):
keys = d.keys()
keys.sort()
for key in keys:
print "%3d %10s : %s" % (d[key][1], key, d[key][0])
def putkeys(mDict, mList):
" put keys only in a dict while preserving entry sequence "
seq = len(mDict)
for key in mList:
mDict[key]=[[],seq]
seq = seq + 1
def putdata(mDict, mList):
" put data in dict by preserved sequence "
for key, data in mDict.items():
data[0]=mList[data[1]]
mDict[key] = data
myDict = {}
myKeys = ['Abel', 'Dexter', 'Francis', 'Matthew', 'Samantha', 'Tyrone']
myData = ['lebA', 'rexteD', 'sicnarF', 'wehttaM', 'ahtnamaS', 'enoryT']
putkeys(myDict, myKeys)
putdata(myDict, myData)
myNewKeys = ['AAbel', 'DDexter', 'FFrancis', 'MMatthew', 'SSamantha',
'TTyrone']
myCompleteData = ['lebA', 'rexteD', 'sicnarF', 'wehttaM', 'ahtnamaS',
'enoryT', 'lebAA', 'rexteDD', 'sicnarFF', 'wehttaMM', 'ahtnamaSS',
'enoryTT']
putkeys(myDict, myNewKeys)
putdata(myDict, myCompleteData)
display(myDict)
#-------End Code Fragment--------
--
Emile van Sebille
emile at fenx.com
-------------------
Ken Power <iam at not.you> wrote in message
news:383529e0.682634 at news1.mysolution.com...
> After reading my post I realized I undervalued what I am really
> attempting to accomplish. Thank you everyone for there help to this
> point. The info about hash values now makes sense. Ok, here is what I
> am really trying to do:
>
> Give a value to a key from a list. However, the values in a list are
> in a prescribed order and must be linked to a specific key in the
> dictionary. I tried to demonstrate that in the previous post, linking
> the list values (names spelled backward) to a specific key in the
> dictionary. In my example, I wanted the names to match. Understandably
> if I placed the values in a dictionary in the first place, I could use
> the keys to match each other, however that would be ridiculous :),
> especially since my goal is 'dynamicly' created dictionaries
> (dictionaries created during run-time, I guess). O well, I ramble
> now.
> Here is what I wanted as output from the previous example:
> >>>> for key in myDict.keys():
> > print key, myDict[key]
> >
> >
> >Tyrone enoryT
> >Samantha ahtnamaS
> >Francis sicnarF
> >Abel lebA
> >Matthew wehttaM
> >Dexter retxeD
>
> Guess I'll have to approach the problem from another angle......
> --------------------------------
> Ken Power
> uncle_wiggly at bigfoot dot com
> --------------------------------
>
> --
> http://www.python.org/mailman/listinfo/python-list
>
>
More information about the Python-list
mailing list