在Python中计算字数的函数

时间:2019-05-24 19:08:26

标签: python algorithm counter

我刚开始使用Python进行开发,希望看到有关我的字数统计解决方案的评论。

请指出我的错误或最佳解决方案。

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")

0 个答案:

没有答案