我刚开始使用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")