Dictionary enumeration in C#

To enumerate a dictionary you either enumerate the values within it: Dictionary<int, string> dic; foreach(string s in dic.Values) { Console.WriteLine(s); } or the KeyValuePairs foreach(KeyValuePair<int, string> kvp in dic) { Console.WriteLine(“Key : ” + kvp.Key.ToString() + “, Value : ” + kvp.Value); } or the keys foreach(int key in dic.Keys) { Console.WriteLine(key.ToString()); } If you … Read more

Difference between dictionary and OrderedDict

As of Python 3.7, a new improvement to the dict built-in is: the insertion-order preservation nature of dict objects has been declared to be an official part of the Python language spec. This means there is no real need for OrderedDict anymore 🎉. They are almost the same. Some minor details to consider… Here are … Read more

Using a class versus struct as a dictionary key

Dictionary<TKey, TValue> uses an IEqualityComparer<TKey> for comparing the keys. If you do not explicitly specify the comparer when you construct the dictionary, it will use EqualityComparer<TKey>.Default. Since neither MyClass nor MyStruct implement IEquatable<T>, the default equality comparer will call Object.Equals and Object.GetHashCode for comparing instances. MyClass is derived from Object, so the implementation will use … Read more

Disabling sorting mechanism in pprint output

Python 3.8 or newer: Use sort_dicts=False: pprint.pprint(data, sort_dicts=False) Python 3.7 or older: You can monkey patch the pprint module. import pprint pprint.pprint({“def”:2,”ghi”:3,”abc”:1,}) pprint._sorted = lambda x:x # Or, for Python 3.7: # pprint.sorted = lambda x, key=None: x pprint.pprint({“def”:2,”ghi”:3, “abc”:1}) Since the 2nd output is essentiallly randomly sorted, your output may be different from mine: … Read more

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