LINQ Group By into a Dictionary Object
Dictionary<string, List<CustomObject>> myDictionary = ListOfCustomObjects .GroupBy(o => o.PropertyName) .ToDictionary(g => g.Key, g => g.ToList());
Dictionary<string, List<CustomObject>> myDictionary = ListOfCustomObjects .GroupBy(o => o.PropertyName) .ToDictionary(g => g.Key, g => g.ToList());
Probably the two most common self balancing tree algorithms are Red-Black trees and AVL trees. To balance the tree after an insertion/update both algorithms use the notion of rotations where the nodes of the tree are rotated to perform the re-balancing. While in both algorithms the insert/delete operations are O(log n), in the case of … Read more
Use dict.setdefault(): dict.setdefault(key,[]).append(value) help(dict.setdefault): setdefault(…) D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
Warning: This answer is contested. See comments. def doAppend( size=10000 ): result = [] for i in range(size): message= “some unique object %d” % ( i, ) result.append(message) return result def doAllocate( size=10000 ): result=size*[None] for i in range(size): message= “some unique object %d” % ( i, ) result[i]= message return result Results. (evaluate each … Read more
You use the collection initializer syntax, but you still need to make a new Dictionary<string, string> object first as the shortcut syntax is translated to a bunch of Add() calls (like your code): var data = new Dictionary<string, string> { { “test”, “val” }, { “test2”, “val2” } }; In C# 6, you now have … Read more
Haven’t tested this very extensively, but works in Python 2.5.2. >>> d = {“x”:2, “h”:15, “a”:2222} >>> it = iter(sorted(d.iteritems())) >>> it.next() (‘a’, 2222) >>> it.next() (‘h’, 15) >>> it.next() (‘x’, 2) >>> If you are used to doing for key, value in d.iteritems(): … instead of iterators, this will still work with the solution … Read more
You have two choices: The first and most performant is to use associateBy function that takes two lambdas for generating the key and value, and inlines the creation of the map: val map = friends.associateBy({it.facebookId}, {it.points}) The second, less performant, is to use the standard map function to create a list of Pair which can … Read more
What is the best way to implement nested dictionaries in Python? This is a bad idea, don’t do it. Instead, use a regular dictionary and use dict.setdefault where apropos, so when keys are missing under normal usage you get the expected KeyError. If you insist on getting this behavior, here’s how to shoot yourself in … Read more
You should iterate over keys with: for key in mydictionary: print “key: %s , value: %s” % (key, mydictionary[key])
b = dict(zip(a[::2], a[1::2])) If a is large, you will probably want to do something like the following, which doesn’t make any temporary lists like the above. from itertools import izip i = iter(a) b = dict(izip(i, i)) In Python 3 you could also use a dict comprehension, but ironically I think the simplest way … Read more