![]() |
Create a new list dynamically - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Create a new list dynamically (/thread-6381.html) |
Create a new list dynamically - floatingshed - Nov-19-2017 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: 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() RE: Create a new list dynamically - stranac - Nov-19-2017 There are ways in which you can do what you describe, but it's a bad idea (although better than your current pickle awfulness). Rather than creating variable names dynamically, you should just be adding your lists to a dictionary. RE: Create a new list dynamically - floatingshed - Nov-19-2017 How would that help? I would still have to go through the dict to find all the entries that contain a particular value. I could just iterate over the original list with it's 13,000 items. The point of making new lists is to shorten the search time. RE: Create a new list dynamically - stranac - Nov-19-2017 There's no big difference between having a list named a and having a list named lists['a']
RE: Create a new list dynamically - floatingshed - Nov-19-2017 I have a list that contains 13,000 entries. I want to create several new lists from it that contain only the proportion of the first list that contains specific data. When a search is performed it would only have to examine the smaller list containing the main criterion. So I would be checking a list of say 1000 items instead of 13,000. RE: Create a new list dynamically - stranac - Nov-20-2017 I see you don't understand what I'm suggesting, maybe example code would help? >>> from collections import defaultdict >>> lists = defaultdict(list) >>> main_list = range(10000) >>> >>> for element in main_list: ... if element % 50 == 0: ... lists['a'].append(element) ... if element % 1000 == 1: ... lists['b'].append(element) ... >>> lists['a'] [0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950, 1000, 1050, 1100, 1150, 1200, 1250, 1300, 1350, 1400, 1450, 1500, 1550, 1600, 1650, 1700, 1750, 1800, 1850, 1900, 1950, 2000, 2050, 2100, 2150, 2200, 2250, 2300, 2350, 2400, 2450, 2500, 2550, 2600, 2650, 2700, 2750, 2800, 2850, 2900, 2950, 3000, 3050, 3100, 3150, 3200, 3250, 3300, 3350, 3400, 3450, 3500, 3550, 3600, 3650, 3700, 3750, 3800, 3850, 3900, 3950, 4000, 4050, 4100, 4150, 4200, 4250, 4300, 4350, 4400, 4450, 4500, 4550, 4600, 4650, 4700, 4750, 4800, 4850, 4900, 4950, 5000, 5050, 5100, 5150, 5200, 5250, 5300, 5350, 5400, 5450, 5500, 5550, 5600, 5650, 5700, 5750, 5800, 5850, 5900, 5950, 6000, 6050, 6100, 6150, 6200, 6250, 6300, 6350, 6400, 6450, 6500, 6550, 6600, 6650, 6700, 6750, 6800, 6850, 6900, 6950, 7000, 7050, 7100, 7150, 7200, 7250, 7300, 7350, 7400, 7450, 7500, 7550, 7600, 7650, 7700, 7750, 7800, 7850, 7900, 7950, 8000, 8050, 8100, 8150, 8200, 8250, 8300, 8350, 8400, 8450, 8500, 8550, 8600, 8650, 8700, 8750, 8800, 8850, 8900, 8950, 9000, 9050, 9100, 9150, 9200, 9250, 9300, 9350, 9400, 9450, 9500, 9550, 9600, 9650, 9700, 9750, 9800, 9850, 9900, 9950] >>> lists['b'] [1, 1001, 2001, 3001, 4001, 5001, 6001, 7001, 8001, 9001] RE: Create a new list dynamically - floatingshed - Nov-20-2017 Oh, now I get it, sorry. That'll work. I'll have a play. Thanks. |