1
\$\begingroup\$

I am new to developing in Python and would like to see comments about my word count solution.

Please indicate my mistakes or best solutions.

def count_words(text):
  text = text.lower()

  symbols = "1234567890!#$%^&*\"()_+=-{}[].,:"
  for s in list(symbols):
    text = text.replace(s,"")
  text = text.split()
  lst = list(set(text))
  dictonary = {"words":lst, "nums":[0]*len(lst)}
  for word in text:
    dictonary['nums'][dictonary['words'].index(word)] += 1

  import pandas as pd  
  df = pd.DataFrame(dictonary)
  return df.sort_values(by=['nums'], ascending=False)#.to_csv("./words.csv")

count_words("Hello world, World War, War and Peace")
\$\endgroup\$

migrated from stackoverflow.com May 24 at 22:01

This question came from our site for professional and enthusiast programmers.

2
\$\begingroup\$

How about using collections.Counter:

import string
from collections import Counter


def count_words(text):
    text = text.lower()
    for s in string.digits + string.punctuation:
        text = text.replace(s, "")
    text = text.split()
    return Counter(text)


print(count_words("Hello world, World War, War and Peace"))
\$\endgroup\$
0
\$\begingroup\$

Thanks for recommending Counter I made several measurements: With the dictionary (without Counter) quite a bit faster

%timeit count_words_pandas("Hello world, World War, War and Peace")
%timeit count_words_nopandas("Hello world, World War, War and Peace")
%timeit count_words_cntr("Hello world, World War, War and Peace")

928 µs ± 6.95 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

8.26 µs ± 13.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

10.5 µs ± 96.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In the future, I will consider the recommendation about the codereview

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy