1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| from pyecharts import options as opts from pyecharts.charts import Bar
def gettext(): txt = open("结果50.txt","r",errors='ignore').read() txt = txt.lower() for ch in '!"#$&()*+,-./:;<=>?@[\\]^_{|}·~‘’': txt = txt.replace(ch,"") return txt txt = gettext() words = txt.split() print(words) counts = {} for word in words: counts[word] = counts.get(word,0) + 1
items = list(counts.items()) items.sort(key=lambda x:x[1],reverse=True)
words = [] counts = [] for i in range(20): words.append(items[i][0]) counts.append(items[i][1])
c = ( Bar() .add_xaxis(words) .add_yaxis("次数", counts) .reversal_axis() .set_series_opts(label_opts=opts.LabelOpts(position="right")) .set_global_opts(title_opts=opts.TitleOpts(title="英文词出现字数")) .render("英文词出现字数.html") )
|