I am struggling to create an order of string using counter in python and my program is not printing an order of the given string below.
from collections import Counter, defaultdict
def checking_anagram(keywords):
agrms = defaultdict(list)
for i in keywords:
hist = tuple(Counter(i).items())
agrms[hist].append(i)
return list(agrms.values())
keywords = ("eat","tea","tan","ate","nat","bat","bat")
print(checking_anagram(keywords))
The only real problem you have is your
histline. I don't know what you were trying to do, but that is wrong. What you need to do is sort the letters of each word, and use that as your key:Output: