dict={'a':1,'b':2,'c':3,'d':4}
for i in dict.items():
print(i)
for i in dict.keys():
print(i)
for i in dict.values():
print(i)
for i,j in dict.items():
print(i,j)
import pandas as pd
pd.DataFrame(data=word).to_csv('big.csv',encoding='utf-8')
線上工具生成詞雲:
https://wordart.com/create
代碼如下:
import pandas as pd
file=open('artical.txt',encoding='utf-8')
text=file.read()
text=text.lower()
for i in str('''?!",.'''):
text=text.replace(i,'')
text=text.split()
# 統計單詞數量
exclude = ['a', 'the', 'and', 'if', 'you', 'in', 'but', 'not', 'it', ' s', 'if', "i"]
dict={}
for i in text:
if i not in exclude:
if i not in dict:
dict[i]=text.count(i)
print(dict)
# 排序單詞數量
word=list(dict.items())
word.sort(key=lambda x: x[1], reverse=True)
print(word)
# 輸出前二十位的單詞
for i in range(20):
print(word[i])
pd.DataFrame(data=word).to_csv('b.csv',encoding='utf-8')