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
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
After iterating through words
wordToAnagram(dictionary)
looks like this
{
"ab" : ["ab", "ba"]
"aabs": ["absa", "sbaa", "base"]
}
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'])])
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]
)