| | |
keys problem
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: May 2008
Posts: 16
Reputation:
Solved Threads: 3
OK guys, I've hit a wall & can't find any info on what I'm wanting to do.
Maybe someone can help me out here or point me in the right direction please?
I have a shelve object- book.
If I do book.keys(), naturally I get a list of the keys- ['key1', 'key2', etc...]
When I do a book['key1'].keys(), I again get a list of those keys- ['keya', 'keyb', etc...]
Is there a way to find out which key a key belongs to?
like - keyb belongs to what key? (keyb belongs to key1)
Thanks
Maybe someone can help me out here or point me in the right direction please?
I have a shelve object- book.
If I do book.keys(), naturally I get a list of the keys- ['key1', 'key2', etc...]
When I do a book['key1'].keys(), I again get a list of those keys- ['keya', 'keyb', etc...]
Is there a way to find out which key a key belongs to?
like - keyb belongs to what key? (keyb belongs to key1)
Thanks
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
Well, that question may not have an answer if the values are not unique; for example, if
book['key1'] = {'keya':valuea, 'keyb':valueb}
and
book['key2'] = {'keya':valuec, 'keyd':valued}
then 'keya' cannot be mapped backwards.
But suppose that your values are unique. Then you could do this:
Basically, you're just reversing a dictionary. But again, you must be mathematically certain that your values are unique; else, you'll clobber things.
Jeff
book['key1'] = {'keya':valuea, 'keyb':valueb}
and
book['key2'] = {'keya':valuec, 'keyd':valued}
then 'keya' cannot be mapped backwards.
But suppose that your values are unique. Then you could do this:
Python Syntax (Toggle Plain Text)
>>> import shelve ## set up an example >>> f= shelve.open("mydatabase") >>> f['one'] = {1:2,3:4} >>> f['two'] = {5:6,7:8} >>> f.close() >>> revdict = {} ## Here's the reverse lookup code >>> for key in f.keys(): for subkey in f[key]: revdict[subkey] = key >>> revdict {1: 'one', 3: 'one', 5: 'two', 7: 'two'}
Basically, you're just reversing a dictionary. But again, you must be mathematically certain that your values are unique; else, you'll clobber things.
Jeff
![]() |
Similar Threads
- Dynamic Declaration of Arrays Keys (PHP)
- Keyboard keys problem (Windows NT / 2000 / XP)
- Windows XP Pro on a Laptop - Black Screen Problem - Help (Windows NT / 2000 / XP)
- XP Explorer Problem (Windows NT / 2000 / XP)
- Acer laptop keys not working (Troubleshooting Dead Machines)
- Can't get multimedia keys to work on mac keyboard (Apple Hardware)
Other Threads in the Python Forum
- Previous Thread: Running Python Code in a Webpage
- Next Thread: Eratosthenes sieve
Views: 487 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied alarm aliased basic beginner casino character code corners cx-freeze definedlines development dictionary digital dynamic editing error events examples excel exe file filename float format ftp function graphics gui handling homework ideas iframe import input java line linux list lists logging loop matching mouse newb number numbers numeric output parameters parsing path port prime program programming progressbar projects py py2exe pygame pyglet pyqt pysimplewizard python random recursion recursive return reverse schedule scrolledtext searchingfile shebang skinning sprite ssh statistics stdout string strings table tails terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 variable voip windows wxpython





