I'm interested in Python 3 code that writes a hash to a disk file as a hash and then code that imports it into a script directly as a hash.
If importing wouldn't work then I'd be content to open and read a file, but I'd prefer not to have to rebuild a hash from a list every single time. I know that creating a list from a file is trivial, but searching a list is prohibitively slow in my script, so I want to use a hash because of the faster search. I don't actually need key-value pairs, just a list, and the hash would be purely to benchmark execution speeds at first. Thanks for all replies.
In order to dump an object (such as a dictionary) into a file in a nice pythonic way, you can use the "pickle" module. For example:
import pickle
mydic={"k1":[1,2,3],"k2":[6,6,6],"k3":"cats"}
f=open("./somefile.bin","wb")
pickle.dump(mydic,f)
You can then load the dumped object using pickel.load(), as described in the python docs (other options are specified as well): https://docs.python.org/2/library/pickle.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.