Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. # Write a function that takes a list of words as input, and returns a list of those words bucketized by anagrams.
  2. # Example:
  3. # Input: ["star", "rats", "car", "arc", "arts", "stars"]
  4. # Output: [ ["star", "rats", "arts"], ["car", "arc"], ["stars"] ]
  5. # [edit]
  6. from collections import defaultdict
  7.  
  8. def vish(words):
  9.  
  10. final_dict=defaultdict(list)
  11. for word in words:
  12. sorted_w=''.join(sorted(word))
  13. print (sorted_w)
  14.  
  15.  
  16. final_dict[sorted_w].append(word)
  17.  
  18. print(final_dict.values())
  19.  
  20.  
  21.  
  22. if __name__=="__main__":
  23. vish(["star", "rats", "car", "arc", "arts", "stars"])
  24.  
  25.  
  26. Output:
  27.  
  28. dict_values([['car', 'arc'], ['stars'], ['star', 'rats', 'arts']])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement