rinki kumari
rinki kumari

Reputation: 9

find the first n most frequent characters in it

Given a string, you have to find the first n most frequent characters in it. If there are two letters with the same frequency, then the alphabetically earlier value should be picked first:

string=  "aabbccc"
n =2
list = []
#write your code here
char_dict = {}

for char in string:
    if char not in char_dict:
        char_dict[char] = 1
    else:
        char_dict[char] += 1


sorted_dict=sorted(char_dict.items(), key=lambda x: (x[1],x[0]))

sorted_dict = sorted_dict[-2:]
for key, value in sorted_dict:
    list.append(key)
print(list)

My output is ['b', 'c'] but it should actually be c and a.

Upvotes: 0

Views: 4213

Answers (6)

RiBi
RiBi

Reputation: 883

string="aabbccc"
unique_chars = list(set(string))
count_chars = map(lambda i: string.count(i), unique_chars)

order = sorted(zip(unique_chars, count_chars), key=lambda x: (-x[1], x[0]))
n=2
nth=order[:n]

the variable order is basically mapping each letter to its count

or in 1 line

sorted(zip(list(set(string)), map(lambda i: string.count(i),list(set(string)))), key=lambda x: (-x[1], x[0]))[:2]

The result is in the following format

[('c', 3), ('a', 2)]

Upvotes: 0

Deva
Deva

Reputation: 1

string="good"
dictionary = {}
n=2
for char in string:
   if(char in dictionary. keys()):
      dictionary[char]+=1
   else:
      dictionary[char]=1
duplicate=[]
for char in dictionary:
   if(dictionary[char] ==n):
      print(char)
      

Upvotes: 0

Abhishek Sa
Abhishek Sa

Reputation: 1

def char_frequency(string,n):
  letter_freq = dict()
  for letter in string:
    if letter not in letter_freq.keys():
      letter_freq[letter] = 1
    else:
      letter_freq[letter] += 1

  list_of_tuples = sorted(letter_freq.items(), key=lambda x: (-x[1],x[0]))[:n]
  print(list_of_tuples)
  final_list = []
  for tup in list_of_tuples:
    final_list.append(tup[0])
  return(sorted(final_list))

print(char_frequency("aabbccc",2))

Upvotes: 0

Sunil
Sunil

Reputation: 21

Below is one of the perfect solution for your problem(with less code)

string=input()
n=int(input())
import collections
out=[collections.Counter(string).most_common(i+1)[i][0] for i in range(n)]
out.sort()
print(out)

Upvotes: 2

Aryerez
Aryerez

Reputation: 3495

The problem is with the sorting. You need to sort by two fields in different directions (1 ascending and 1 descending). Change the sorted_dict 2 lines to:

sorted_dict = sorted(char_dict.items(), key=lambda x: (-x[1], x[0]))
sorted_dict = sorted_dict[:n]

btw: Avoid using python's keywords (such as list) as variable names. Name it myList or something similar.

Upvotes: 5

Frank
Frank

Reputation: 2029

I added the print outputs after the statement. The code should be selfdescripting.

from collections import defaultdict

string=  "aabbccc"
n = 2

result = defaultdict(int)
for char in string:
    result[char] += 1
print(result)  # defaultdict(<class 'int'>, {'b': 2, 'a': 2, 'c': 3})

ordered_result = sorted(result.items(), key=lambda x: (-x[1], x[0]))
print(ordered_result)  # [('c', 3), ('a', 2), ('b', 2)]

ordered_list = [x[0] for x in ordered_result]
print(ordered_list[:n])  # ['c', 'a']

Upvotes: 1

Related Questions