Know the depth of a dictionary
You need to create a recursive function: >>> def depth(d): … if isinstance(d, dict): … return 1 + (max(map(depth, d.values())) if d else 0) … return 0 … >>> d = {‘a’:1, ‘b’: {‘c’:{}}} >>> depth(d) 3
You need to create a recursive function: >>> def depth(d): … if isinstance(d, dict): … return 1 + (max(map(depth, d.values())) if d else 0) … return 0 … >>> d = {‘a’:1, ‘b’: {‘c’:{}}} >>> depth(d) 3
Section 23.1.2#8 (associative container requirements): The insert members shall not affect the validity of iterators and references to the container, and the erase members shall invalidate only iterators and references to the erased elements. So yes storing pointers to data members of a map element is guaranteed to be valid, unless you remove that element.
Update (Back to the future): with C++11 range-based for loops – std::map<Key, Value> m { … /* initialize it */ … }; for (const auto &p : m) { std::cout << “m[” << p.first << “] = ” << p.second << ‘\n’; }
The architect means that get and containsKey have the same costs and may be accumulated into one check: Integer val = map.get(c); if (val != null) { … } else { … } But I wonder why the architect is only concerned about that, as there are more things to improve: Refer to objects by … Read more
For me, this worked: cursor = conn.cursor(dictionary=True) Detailed example: import mysql.connector # pip install mysql-connector-python conn = mysql.connector.connect(host=”localhost”, user=”user”, passwd=”pass”, database=”dbname”) cursor = conn.cursor(dictionary=True) sql = “SELECT * FROM `table` WHERE 1” cursor.execute(sql) rows = cursor.fetchall() for row in rows: row[“col”]
If you really don’t want to import pprint but want it to “look like” a dictionary, you could do: print(“{” + “\n”.join(“{!r}: {!r},”.format(k, v) for k, v in d.items()) + “}”)
You can use your_dict.get(key, “default value”) instead of directly referencing a key.
They are semantically different. Consider: #include <set> #include <map> #include <utility> #include <iostream> using namespace std; int main() { pair<int, int> p1(1, 1); pair<int, int> p2(1, 2); set< pair<int, int> > s; s.insert(p1); s.insert(p2); map<int, int> m; m.insert(p1); m.insert(p2); cout << “Set size = ” << s.size() << endl; cout << “Map size = ” … Read more
You can fill the dictionary using reflection: public Dictionary<String, Object> Dyn2Dict(dynamic dynObj) { var dictionary = new Dictionary<string, object>(); foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(dynObj)) { object obj = propertyDescriptor.GetValue(dynObj); dictionary.Add(propertyDescriptor.Name, obj); } return dictionary; }
You could write your own read-only wrapper for the dictionary, e.g.: public class ReadOnlyDictionaryWrapper<TKey, TValue, TReadOnlyValue> : IReadOnlyDictionary<TKey, TReadOnlyValue> where TValue : TReadOnlyValue { private IDictionary<TKey, TValue> _dictionary; public ReadOnlyDictionaryWrapper(IDictionary<TKey, TValue> dictionary) { if (dictionary == null) throw new ArgumentNullException(“dictionary”); _dictionary = dictionary; } public bool ContainsKey(TKey key) { return _dictionary.ContainsKey(key); } public IEnumerable<TKey> Keys … Read more