3
a = ['ab', 'absa', 'sbaa', 'basa', 'ba']
res = []
s = 0
for i in range(len(a)):
    b=a[i]
    c = ''.join(sorted(b))
    res.append(c)
res.sort(reverse=False)
wordfreq = [res.count(p) for p in res]
d = dict(zip(res, wordfreq))
all_values = d.values()  #all_values is a list
max_value = max(all_values)
print(max_value)
max_key = max(d, key=d.get)
print(max_key)

In the given problem user inputs various anagram words, the output should be the maximum frequency of that word and print those anagrams. If you please help me print those anagrams from the input it will be really helpful.

Ooutput:

3 aabs       

Expected Ooutput:

3
absa sbaa basa
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153

2 Answers2

3

You can create a dictionary of word v/s list of anagrams

and then print out the word which contains the maximum number of elements in the anagram list

from collections import defaultdict
words = ['ab','absa','sbaa','basa','ba']
wordToAnagram= defaultdict(list) 
# word vs list anagram 
# loop below will create {aabs:  ['absa', 'sbaa', 'basa']}
for word in words:
    s = "".join(sorted(word))
    wordToAnagram[s].append(word)


word, anagrams = max(wordToAnagram.items(), key=lambda x: len(x[1]))
print(" ".join(anagrams))

OUTPUT:

3
absa sbaa basa

Details

  1. wordToAnagrams

After iterating through words wordToAnagram(dictionary) looks like this

{
"ab" : ["ab", "ba"]
"aabs":  ["absa",  "sbaa", "base"]
}
  1. dictionary.items()

wordToAnagram.items() returns tuple-pair of dictionary key-value

where,

key: is our sorted string "ab" or "aabs",

value : is list of anagrams, e.g for key = "ab", value equals["ab", "ba"]

dict_items([('ab', ['ab', 'ba']), ('aabs', ['absa', 'sbaa', 'base'])])
  1. max function using 'key' and lambda expression

max(wordToAnagram.items(), key=lambda x: len(x[1]))

finds maximum value from wordToAnagram.items() iterable, by comparing length of anagrams list (len(x[1])

Anurag Wagh
  • 1,086
  • 6
  • 16
  • If you please explain this line being a newbie its quite difficult to comprehend `word, anagrams = max(wordToAnagram.items(), key=lambda x: len(x[1]))` – Ritesh Mukhopadhyay Jun 07 '20 at 13:53
2

You can try with numpy and mode from statistics module

import numpy as np
from statistics import mode

words = ['ab','absa','sbaa','basa','ba']

# This sorts the letters of each word, and creates a list of them
sorted_words = [''.join(sorted(word)) for word in words]


max_freq_anagrams = np.array(words)[np.array(sorted_words) == mode(sorted_words)]
# mode(sorted_words) gives you the (sorted) word with the highest frequency
# np.array(sorted_words) == mode(sorted_words) gives you a list of true/false 
# and finaly you slice your words by this true/false list


print(len(max_freq_anagrams))
print(list(max_freq_anagrams))

In case you have multiple max frequent words e.g. words = ['ab','absa','sbaa','basa','ba', 'ba']

then instead of mode(sorted_words) use max(set(sorted_words), key=sorted_words.count) which takes the first most frequent word.

dimitris_ps
  • 5,849
  • 3
  • 29
  • 55