Is it possible to apply sort within many dictionary?

I try to do 5th Week’s 2nd problem in my way.
with my code I made Individual Dictionaries,bur Can’t apply sort in my code.
is it possible to apply sort with in many dictionaries ??
what should I do?

poem=open("rpoem.txt","r")
poem_read=poem.read()
poem1=poem_read.lower()

j=0
temp=""
d={""}
dic={}
read_line=poem1.split()
len_read_line=len(read_line)
for i in range (len_read_line):
    if read_line[i] not in temp:
        temp+=read_line[i]
        # print(j,"\t",read_line[i],"\t\t",(read_line.count(read_line[i])))
        dic={read_line[i]:read_line.count(read_line[i])}
        d=dic
        print(j,"\t",dic)
        j+=1 

https://colab.research.google.com/drive/1zL6W_x-d7ppFPWB6m5-uXbXt-lWhP7jS#scrollTo=7tnJRaHqho1-&line=6&uniqifier=1

@Sanjit_Ghosh
Could you please elaborate what are you trying to achieve?

Dictionary dic will contain one key:value pair only.
As you are assigning d = dic , you are essentially overwriting the dictionary d, in fact d just points to a new dic each time.
Finally, the dictionary d is containing only one key:value pair.
For meaningful sorting, you need more than one key:value pair.

Hence, would like to know what is the expected outcome of the implementation?

@pandurang
Sir,as you can See the output,my code build separate dictionaries for every word with their value.
After that this is very difficult to find out most frequent word in my text.
That’s why I just want to sort every dictionaries by there value.
So I just want to know is it possible to short many dictionaries by their value?
If yes,then what I need to do???
Thank you sir for paying attention.

As I mentioned earlier,
if there is only one key:value pair in a dictionary it is meaningless to sort that dictionary.
My suggestion here is you should not build individual dictionaries.
Rather, a single dictionary with several key:value pairs.
Use this hint to improve / tweak the logic a bit.

Keep d as your main dictionary, add 'new_word':count to it
And then try sorting it.

1 Like