I have a scenario where i stuck not getting how do i need to implement.
I have string need to find count of each letter -> ('a',2)
. Sort by second value which is count and order by first if same count for other letter as well.
My code :
s = 'aabbbccde'
c = re.finditer(r'([w])(1*)',s)
lst = []
for i in c:
lst.append((list(map(str,set(i.__getitem__(0))))[0] , len(i.__getitem__(0))))
var = sorted(
lst,
key=lambda x: x[1],reverse=True)
for i in sorted( (sorted(var,key=lambda x:x[1],reverse=True)) , key=lambda x:x[0])[:3]:
print(i[0],i[1])
My output :
a 2
b 3
c 2
I am not getting as expected output :
b 3
a 2
c 2
Where i am going wrong in my code or is their any efficent way to do it …
New contributor
quora question is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.