What is the difference between dict.items() and dict.iteritems() in Python2?

It’s part of an evolution. Originally, Python items() built a real list of tuples and returned that. That could potentially take a lot of extra memory. Then, generators were introduced to the language in general, and that method was reimplemented as an iterator-generator method named iteritems(). The original remains for backwards compatibility. One of Python … Read more

Get key by value in dictionary

mydict = {‘george’: 16, ‘amber’: 19} print mydict.keys()[mydict.values().index(16)] # Prints george Or in Python 3.x: mydict = {‘george’: 16, ‘amber’: 19} print(list(mydict.keys())[list(mydict.values()).index(16)]) # Prints george Basically, it separates the dictionary’s values in a list, finds the position of the value you have, and gets the key at that position. More about keys() and .values() in … Read more

How do you sort a dictionary by value?

Use LINQ: Dictionary<string, int> myDict = new Dictionary<string, int>(); myDict.Add(“one”, 1); myDict.Add(“four”, 4); myDict.Add(“two”, 2); myDict.Add(“three”, 3); var sortedDict = from entry in myDict orderby entry.Value ascending select entry; This would also allow for great flexibility in that you can select the top 10, 20 10%, etc. Or if you are using your word frequency … Read more

Why dict.get(key) instead of dict[key]?

It allows you to provide a default value if the key is missing: dictionary.get(“bogus”, default_value) returns default_value (whatever you choose it to be), whereas dictionary[“bogus”] would raise a KeyError. If omitted, default_value is None, such that dictionary.get(“bogus”) # <– No default specified — defaults to None returns None just like dictionary.get(“bogus”, None) would.

Convert a String representation of a Dictionary to a dictionary

You can use the built-in ast.literal_eval: >>> import ast >>> ast.literal_eval(“{‘muffin’ : ‘lolz’, ‘foo’ : ‘kitty’}”) {‘muffin’: ‘lolz’, ‘foo’: ‘kitty’} This is safer than using eval. As its own docs say: >>> help(ast.literal_eval) Help on function literal_eval in module ast: literal_eval(node_or_string) Safely evaluate an expression node or a string containing a Python expression. The string … Read more

Collection was modified; enumeration operation may not execute

What’s likely happening is that SignalData is indirectly changing the subscribers dictionary under the hood during the loop and leading to that message. You can verify this by changing foreach(Subscriber s in subscribers.Values) To foreach(Subscriber s in subscribers.Values.ToList()) If I’m right, the problem will disappear. Calling subscribers.Values.ToList() copies the values of subscribers.Values to a separate … Read more

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