Skip to content

Instantly share code, notes, and snippets.

@alexandre
Last active August 29, 2015 14:08
Show Gist options
  • Save alexandre/f03f98a8e5ba9fddd0be to your computer and use it in GitHub Desktop.
Save alexandre/f03f98a8e5ba9fddd0be to your computer and use it in GitHub Desktop.
word counter
from itertools import groupby
def word_counter(*words):
'''Write a Python program that inputs a list of words, separated by white-
space, and outputs how many times each word appears in the list.
'''
return {word: len(list(word_group)) for word, word_group in
groupby(sorted(words), key=lambda x: x)}
@alexandre
Copy link
Author

Outra versão ainda mais simples:

import collections


def word_counter(*string):
    return dict(collections.Counter(string).items())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment