Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Write a function that takes a list of words as input, and returns a list of those words bucketized by anagrams.
- # Example:
- # Input: ["star", "rats", "car", "arc", "arts", "stars"]
- # Output: [ ["star", "rats", "arts"], ["car", "arc"], ["stars"] ]
- # [edit]
- from collections import defaultdict
- def vish(words):
- final_dict=defaultdict(list)
- for word in words:
- sorted_w=''.join(sorted(word))
- print (sorted_w)
- final_dict[sorted_w].append(word)
- print(final_dict.values())
- if __name__=="__main__":
- vish(["star", "rats", "car", "arc", "arts", "stars"])
- Output:
- dict_values([['car', 'arc'], ['stars'], ['star', 'rats', 'arts']])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement