Yahoo Αναζήτηση Διαδυκτίου

Αποτελέσματα Αναζήτησης

  1. For inverting key-value pairs in your dictionary use a for -loop approach: # Use this code to invert dictionaries that have non-unique values. inverted_dict = dict() for key, value in my_dict.items(): inverted_dict.setdefault(value, list()).append(key) Second Solution.

  2. Dictionaries are unordered so you cannot reverse them. The order of the current output is arbitrary. That said, you can order the keys of course: for i in sorted(a.keys(), reverse=True): print a[i]; but this gives you the reverse order of the sorted keys, not necessarily

  3. 21 Φεβ 2017 · However if you need to do more than one lookup it will be more efficient to create an inverse dictionary. ivd = {v: k for k, v in d.items ()} If there is a possibility of multiple keys with the same value, you will need to specify the desired behaviour in this case. If your Python is 2.6 or older, you can use.

  4. You should have in mind that a python dict must have a unique keys. So, if you know that when reversing keys and values of your current dict will have a unique keys, you can use a simple dict comprehension like this example: {v:k for k,v in my_dict.items()} However, you can use groupby from itertools module like this example: from itertools ...

  5. For your code a simple change will do what you want: def invert_dict(d): inverse = dict() for key in d: # Go through the list that is saved in the dict: for item in d[key]: # Check if in the inverted dict the key exists. if item not in inverse: # If not create a new list.

  6. 25 Αυγ 2017 · Basically, You can use LINQ and get the Key like this, without reversing anything: var key = dictionary.FirstOrDefault(x => x.Value == "ab").Key; If you really want to reverse your Dictionary, you can use an extension method like this: public static Dictionary<TValue, TKey> Reverse<TKey, TValue>(this IDictionary<TKey, TValue> source) {.

  7. 31 Μαΐ 2009 · Briefly create a reversed sorted dictionary in one line. var dict = new SortedDictionary<int, int>(Comparer<int>.Create((x, y) => y.CompareTo(x))); There's a way to create a IComparer<T> using System.Collections.Generic.Comparer<T>. Just pass a IComparision<T> delegate to its Create method to build a IComparer<T>.

  8. Beware that you cannot 'reverse' a dictionary if. More than one key shares the same value. For example {'one':1,'two':1}. The new dictionary can only have one item with key 1. One or more of the values is unhashable. For example {'one':[1]}. [1] is a valid value but not a valid key. See this thread on the python mailing list for a discussion on ...

  9. Whenever one has a dictionary where the values are integers, the Counter data structure is often a better choice to represent the data than a dictionary. If you already have a dictionary, a counter can easily be formed by: c = Counter(d['123']) as an example from your data.

  10. 22 Φεβ 2017 · If you're going to call the function multiple times, it might be worth building out the reverse dictionary the first time through the function, so that you can use that to speed up future lookups. The catch there is that if you do have a ton of data, you're going to build a fairly large data structure in memory. Probably not a big deal, but ...

  1. Γίνεται επίσης αναζήτηση για