iterating through humongously large key lists!!

Stephen Hansen news at myNOSPAM.org
Wed Apr 25 14:23:38 EDT 2001


May I sugguest the following? I don't know if it is any good for you, but
hey. :)

dict = {really-big-dictionary}

try:
    while 1:
        key, value = dict.popitem()
        if value >= 2:
            dict[key] = value
except KeyError:
    pass

--S

"Mark blobby Robinson" <m.1.robinson at herts.ac.uk> wrote in message
news:3AE706FE.719B091F at herts.ac.uk...
> I needed a non-destructive method so popitem was not a viable option, but
I
> was able to use juans suggestion of using the firstkey() and nextkey()
> methods but with gdb. However although I can iterate through the list
> beautifully now I am getting some wierd behaviour which is throwing me. If
I
> run the following code:
>
> x = db.firstkey()
> while x != None:
>              if db[x] < 2:
>                         del db[x]
>              x = db.nextkey[x]
>
>
> I get the following exception:
>
> Traceback (innermost last):
>   File "test.py", line 28, in ?
>     findEligible(filename+'.dat', Dict)
>   File "motif.py", line 252, in findEligible
>     del motiflist[x]
>   File "/usr/lib/python1.5/shelve.py", line 74, in __delitem__
>     del self.dict[key]
> KeyError: CACTG_GCGCC_TATAG
>
> Which seems wierd to me, the key is obviously there cos I have just
extracted
> it using firstkey() and nextkey(). Just to check instead of the line:
del
> db[x]     I tried     print db[x]
> and that doesn't generate a keyError. Why should del db[x] give me a
> keyError?
>
> Blobby
>





More information about the Python-list mailing list