JavaScript面對物件建構函式實戰之drag實現元素拖拽操作,事件繫結中的this指向
阿新 • • 發佈:2020-11-13
import jieba #新增jieba分詞 mydict = ['聶小倩',"寧採臣",'燕赤霞','黑山老妖','辛十四娘'] for item in mydict: jieba.add_word(item) txt = open('聊齋志異.txt',"r", encoding='utf-8').read() #注意這裡的路徑要用\\,因為在python中\r表示表示將游標的位置回退到本行的開頭 #定義別名列表 bieming = [["聶小倩","小倩"],[ "寧採臣" ],['燕赤霞'],['黑山老妖'],['辛十四娘']] words=jieba.lcut(txt) counts={} for word in words: if len(word) ==1: continue else: counts[word] = counts.get(word,0)+1 #計算出場次數(各個別名的合計次數) lst=list() for i in range(5): lt=0 for item in bieming[i]: lt += counts.get(item,0) lst.append(lt) twelvechai= ['聶小倩',"寧採臣",'燕赤霞','黑山老妖','辛十四娘'] items=list() for i in range(5): items.append([twelvechai[i],lst[i]]) items.sort(key=lambda x:x[1], reverse=True) for i in range(5): word, count = items[i] print ("{0:<10}{1:>5}".format(word, count))