Nov-19-2017, 08:23 AM
(This post was last modified: Nov-19-2017, 08:23 AM by floatingshed.)
I have a list of dicts. One of the values in the dict contains several words semi-colon separated which I would like to be the names of dynamically created lists. I can iterate over the master list and separate the required list names but cannot figure out how to create and name the lists. e.g.
dict1 contains a,b,c I need to create lists a b and c and copy dict1 into each.
dict2 contains a,c,g I need to append dict2 to lists a and c, and create list g then copy dict2 into it.
At the moment I achieve this by creating pickled items on disc, as creating named files dynamically is a breeze. But as the master list grows the process slows...a lot!
I'd appreciate a pointer or two, thanks.
This is the disc/pickle version:
dict1 contains a,b,c I need to create lists a b and c and copy dict1 into each.
dict2 contains a,c,g I need to append dict2 to lists a and c, and create list g then copy dict2 into it.
At the moment I achieve this by creating pickled items on disc, as creating named files dynamically is a breeze. But as the master list grows the process slows...a lot!
I'd appreciate a pointer or two, thanks.
This is the disc/pickle version:
def CreateDict(): with open(music, "rb") as f: try: while True: disc_data = pickle.load(f) except EOFError: pass for i in disc_data: category = (i['categories']).split(';') for cat in category: cat_data=[] cat = cat.lstrip() if cat =="":cat = "No category" cat_dir = db_dir+cat+'.db' if not os.path.isfile(cat_dir):open(cat_dir, 'w').close() with open(cat_dir, "rb") as h: try: while True: cat_data = pickle.load(h) except EOFError: pass cat_data.append(i) output1 = open((cat_dir), 'wb') pickle.dump(cat_data, output1) output1.close()