Elegant way to store dictionary permanently with Python?

Why not dump it to a JSON file, and then load it from there where you need it? import json with open(‘my_dict.json’, ‘w’) as f: json.dump(my_dict, f) # elsewhere… with open(‘my_dict.json’) as f: my_dict = json.load(f) Loading from JSON is fairly efficient. Another option would be to use pickle, but unlike JSON, the files it … Read more

Convert a Dictionary to string of url parameters?

One approach would be: var url = string.Format(“http://www.yoursite.com?{0}”, HttpUtility.UrlEncode(string.Join(“&”, parameters.Select(kvp => string.Format(“{0}={1}”, kvp.Key, kvp.Value))))); You could also use string interpolation as introduced in C#6: var url = $”http://www.yoursite.com?{HttpUtility.UrlEncode(string.Join(“&”, parameters.Select(kvp => $”{kvp.Key}={kvp.Value}”)))}”; And you could get rid of the UrlEncode if you don’t need it, I just added it for completeness.

Pandas groupby.size vs series.value_counts vs collections.Counter with multiple series

There’s actually a bit of hidden overhead in zip(df.A.values, df.B.values). The key here comes down to numpy arrays being stored in memory in a fundamentally different way than Python objects. A numpy array, such as np.arange(10), is essentially stored as a contiguous block of memory, and not as individual Python objects. Conversely, a Python list, … Read more

python: iterating through a dictionary with list values

Here’s a speed test, why not: import random numEntries = 1000000 d = dict(zip(range(numEntries), [random.sample(range(0, 100), 2) for x in range(numEntries)])) def m1(d): for k in d: for x in d[k]: pass def m2(d): for k, dk in d.iteritems(): for x in dk: pass import cProfile cProfile.run(‘m1(d)’) print cProfile.run(‘m2(d)’) # Ran 3 trials: # m1: … Read more

Beginner Python: AttributeError: ‘list’ object has no attribute

Consider: class Bike(object): def __init__(self, name, weight, cost): self.name = name self.weight = weight self.cost = cost bikes = { # Bike designed for children” “Trike”: Bike(“Trike”, 20, 100), # <– # Bike designed for everyone” “Kruzer”: Bike(“Kruzer”, 50, 165), # <– } # Markup of 20% on all sales margin = .2 # Revenue … Read more

How can I set index while converting dictionary to dataframe?

Use set_index: df = pd.DataFrame(dictionary, columns=[‘Date’, ‘Open’, ‘Close’]) df = df.set_index(‘Date’) print (df) Open Close Date 2016/11/22 07:00:00 47.47 47.48 2016/11/22 06:59:00 47.46 47.45 2016/11/22 06:58:00 47.38 47.40 Or use inplace: df = pd.DataFrame(dictionary, columns=[‘Date’, ‘Open’, ‘Close’]) df.set_index(‘Date’, inplace=True) print (df) Open Close Date 2016/11/22 07:00:00 47.47 47.48 2016/11/22 06:59:00 47.46 47.45 2016/11/22 06:58:00 47.38 … Read more

Partial search in HashMap

Yeah, a HashMap is not the right data structure for this. As Bozho said, a Trie would be the right one. With Java’s on-board tools, a TreeMap (or any SortedMap, actually) could be used: public <V> SortedMap<String, V> filterPrefix(SortedMap<String,V> baseMap, String prefix) { if(prefix.length() > 0) { char nextLetter = prefix.charAt(prefix.length() -1) + 1; String … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)