Storing Python dictionaries

Pickle save: try: import cPickle as pickle except ImportError: # Python 3.x import pickle with open(‘data.p’, ‘wb’) as fp: pickle.dump(data, fp, protocol=pickle.HIGHEST_PROTOCOL) See the pickle module documentation for additional information regarding the protocol argument. Pickle load: with open(‘data.p’, ‘rb’) as fp: data = pickle.load(fp) JSON save: import json with open(‘data.json’, ‘w’) as fp: json.dump(data, fp) … Read more

Does VBA have Dictionary Structure?

Yes. Set a reference to MS Scripting runtime (‘Microsoft Scripting Runtime’). As per @regjo’s comment, go to Tools->References and tick the box for ‘Microsoft Scripting Runtime’. Create a dictionary instance using the code below: Set dict = CreateObject(“Scripting.Dictionary”) or Dim dict As New Scripting.Dictionary Example of use: If Not dict.Exists(key) Then dict.Add key, value End … Read more

.NET HashTable Vs Dictionary – Can the Dictionary be as fast?

System.Collections.Generic.Dictionary<TKey, TValue> and System.Collections.Hashtable classes both maintain a hash table data structure internally. None of them guarantee preserving the order of items. Leaving boxing/unboxing issues aside, most of the time, they should have very similar performance. The primary structural difference between them is that Dictionary relies on chaining (maintaining a list of items for each … Read more

Get dictionary value by key

It’s as simple as this: String xmlfile = Data_Array[“XML_File”]; Note that if the dictionary doesn’t have a key that equals “XML_File”, that code will throw an exception. If you want to check first, you can use TryGetValue like this: string xmlfile; if (!Data_Array.TryGetValue(“XML_File”, out xmlfile)) { // the key isn’t in the dictionary. return; // … Read more

How can I loop through a C++ map of maps?

Old question but the remaining answers are outdated as of C++11 – you can use a ranged based for loop and simply do: std::map<std::string, std::map<std::string, std::string>> mymap; for(auto const &ent1 : mymap) { // ent1.first is the first key for(auto const &ent2 : ent1.second) { // ent2.first is the second key // ent2.second is the … Read more

What is more efficient: Dictionary TryGetValue or ContainsKey+Item?

TryGetValue will be faster. ContainsKey uses the same check as TryGetValue, which internally refers to the actual entry location. The Item property actually has nearly identical code functionality as TryGetValue, except that it will throw an exception instead of returning false. Using ContainsKey followed by the Item basically duplicates the lookup functionality, which is the … Read more

Split / Explode a column of dictionaries into separate columns with pandas

To convert the string to an actual dict, you can do df[‘Pollutant Levels’].map(eval). Afterwards, the solution below can be used to convert the dict to different columns. Using a small example, you can use .apply(pd.Series): In [2]: df = pd.DataFrame({‘a’:[1,2,3], ‘b’:[{‘c’:1}, {‘d’:3}, {‘c’:5, ‘d’:6}]}) In [3]: df Out[3]: a b 0 1 {u’c’: 1} 1 … Read more

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