All Questions

1289
votes
45answers
1152855 views

Sort a Map<Key, Value> by values (Java)

I am relatively new to Java, and often find that I need to sort a Map<Key, Value> on the values. Since the values are not unique, I find myself converting the keySet into an array, and sorting t...
2576
votes
38answers
1557796 views

How do I sort a dictionary by value?

I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary. I can sort on the keys, but how...
113
votes
6answers
10003 views

Why is the order in dictionaries and sets arbitrary?

I don't understand how looping over a dictionary or set in python is done by 'arbitrary' order. I mean, it's a programming language so everything in the language must be 100% determined, correct? Pyt...
2763
votes
44answers
986060 views

How to merge two dictionaries in a single expression?

I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The update() method would be what I need, if it returned its result instead of mod...
2225
votes
35answers
1765767 views

How to efficiently iterate over each Entry in a Map?

If I have an object implementing the Map interface in Java and I wish to iterate over every pair contained within it, what is the most efficient way of going through the map? Will the ordering of e...
547
votes
22answers
747853 views

How can I sort a dictionary by key?

What would be a nice way to go from {2:3, 1:89, 4:5, 3:0} to {1:89, 2:3, 3:0, 4:5}? I checked some posts but they all use the "sorted" operator that returns tuples.
171
votes
10answers
171801 views

Python dictionary, how to keep keys/values in same order as declared?

new to Python and had a question about dictionaries. I have a dictionary that I declared in a particular order and want to keep it in that order all the time. The keys/values can't really be kept in o...
1748
votes
12answers
2379491 views

Iterating over dictionaries using 'for' loops

I am a bit puzzled by the following code: d = {'x': 1, 'y': 2, 'z': 3} for key in d: print key, 'corresponds to', d[key] What I don't understand is the key portion. How does Python recognize t...
1187
votes
17answers
448852 views

How do I sort a list of dictionaries by values of the dictionary in Python?

I got a list of dictionaries and want that to be sorted by a value of that dictionary. This [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}] sorted by name, should become [{'name':'Bart', ...
635
votes
11answers
397598 views

Collection was modified; enumeration operation may not execute

I can't get to the bottom of this error, because when the debugger is attached, it does not seem to occur. Below is the code. This is a WCF server in a Windows service. The method NotifySubscribers i...

Previous Next