can we sort dictionary using values?
we have a string with duplicate char like "aaabbddebe" and we have to find the frequency of char in string. I used a counter function to do this but the next condition is found top 3 max frequency and we have to do sort() dictionary in desc order how can I do it?
from collections import Counter import operator import collections strn=input() dct=Counter(strn) a=dct.sort() print(a)
Traceback (most recent call last): File "main.py", line 6, in <module> a=dct.sort() AttributeError: 'Counter' object has no attribute 'sort'
You can't sort a dictionary because it is inheritedly orderless so you need a ordered type to represent sorted values.
With lambda expression:
sorted_x = sorted(x.items(), key=lambda kv: kv)
After that, if you want a dictionary you can do:
import collections sorted_dict = collections.OrderedDict(sorted_x)
How to Sort a Dictionary by Value in Python, Use the sorted function and lambda function. If you do not want to use the operator module, you can use a lambda function: sorted_d = Dictionary is mutable in nature, which means changes can be made even after declaring a dictionary in python. It is unordered and allows duplicate entries, only in the values since the keys has to be distinct. The values are accessed using the keys as indexes in a dictionary. A dictionary is declared in curly brackets.
You don't need to call
sort on anything;
Counter already has a method
most_common(n) which will return what you want:
from collections import Counter c = Counter('aaabbccdddddeeeee') top3 = c.most_common(3) print(top3) # [('e', 5), ('d', 5), ('a', 3)]
Or if you just want the letters:
top3letters = [letter for letter, freq in c.most_common(3)] print(top3letters) # ['e', 'd', 'a']
How to sort a dictionary by values in Python, Third, sort the values alphabetically using key_value.iteritems(), key = lambda (k, v) : (v, k)) for i in sorted (key_value.keys()) : The best application of dictionary can be seen in Twitter Sentiment Analysis where analysis Twitter sentiments On the other hand, if one wanted to sort a dictionary by value (as is asked in the question), one could do the following: for key, value in sorted(mydict.iteritems(), key=lambda (k,v): (v,k)): print "%s: %s" % (key, value) The result of this command (sorting the dictionary by value) should return the following:
I have an alternate method, we can use the concept of ASCII code.Save the string in a array then convert the string into ascii code and sort them using sort() function. Then get the vales back using ASCII code.
def ASCII(s): x = 0 for i in xrange(len(s)): x += ord(s[i])*2**(8 * (len(s) - i - 1)) return x
to change ASCII value back to string u can use
where list is list of ascii codes.
Python, Sorting Python dictionaries by Values. In the same way as we did with the keys, we can use sorted to sort the Python dictionary by its values: Sort dictionary contents by Value. To sort dictionary elements by value we will use the same sorted() function and pass a key function that will return the 1th index element of tuple i.e. the value field from the key/value pair,
How to Sort Python Dictionaries by Key or Value, You can obtain a sorted list of the keys using the sorted() function on of the key-value tuples of the dictionary, so you can apply the sorted() If you want to sort this dictionary by values (i.e., the age), you must use another data structure such as a list, or an ordered dictionary. Use the sorted function and operator module import operator sorted_d = sorted(d. items(), key =operator. itemgetter(1)) Sorted_d is a list of tuples sorted by the second element in each tuple.
Sorting Dictionaries in Python, A dictionary in Python is a data structure which stores values as a key-value pair. We can sort this type of data by either the key or the value and this is done by using the We will pass the data we retrieved using one of the above methods. We can sort a Dictionary with the Keys and Values properties and a List instance. Some approaches are slower than others.
How to sort a dictionary in Python, How to sort a Python dict (dictionary) by keys or values Or you can use a data structure that preserves order such as a list or the new The following code snippet sorts a Dictionary by values. The code first creates a dictionary and then uses OrderBy method to sort the items. public void SortDictionary ()