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

Memory efficiency: One large dictionary or a dictionary of smaller dictionaries?

Three suggestions: Use one dictionary. It’s easier, it’s more straightforward, and someone else has already optimized this problem for you. Until you’ve actually measured your code and traced a performance problem to this part of it, you have no reason not to do the simple, straightforward thing. Optimize later. If you are really worried about … Read more

Python: Elegantly merge dictionaries with sum() of values [duplicate]

Doesn’t get simpler than this, I think: a=[(“13.5”,100)] b=[(“14.5”,100), (“15.5”, 100)] c=[(“15.5”,100), (“16.5”, 100)] input=[a,b,c] from collections import Counter print sum( (Counter(dict(x)) for x in input), Counter()) Note that Counter (also known as a multiset) is the most natural data structure for your data (a type of set to which elements can belong more than … Read more

How to access the first and the last elements in a dictionary?

Use an OrderedDict, because a normal dictionary doesn’t preserve the insertion order of its elements when traversing it. Here’s how: # import the right class from collections import OrderedDict # create and fill the dictionary d = OrderedDict() d[‘first’] = 1 d[‘second’] = 2 d[‘third’] = 3 # retrieve key/value pairs els = list(d.items()) # … Read more

Key: value store in Python for possibly 100 GB of data, without client/server [closed]

You can use sqlitedict which provides key-value interface to SQLite database. SQLite limits page says that theoretical maximum is 140 TB depending on page_size and max_page_count. However, default values for Python 3.5.2-2ubuntu0~16.04.4 (sqlite3 2.6.0), are page_size=1024 and max_page_count=1073741823. This gives ~1100 GB of maximal database size which fits your requirement. You can use the package … Read more

Python’s hasattr on list values of dictionaries always returns false?

hasattr does not test for members of a dictionary. Use the in operator instead, or the .has_key method: >>> example = dict(foo=’bar’) >>> ‘foo’ in example True >>> example.has_key(‘foo’) True >>> ‘baz’ in example False But note that dict.has_key() has been deprecated, is recommended against by the PEP 8 style guide and has been removed … Read more

How to limit the maximum size of a Map by removing oldest entries when limit reached [closed]

You can use LinkedHashMap like this You can remove by LRU or FIFO. public static <K, V> Map<K, V> createLRUMap(final int maxEntries) { return new LinkedHashMap<K, V>(maxEntries*10/7, 0.7f, true) { @Override protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { return size() > maxEntries; } }; }