python结巴分词后字典排列元素(key⼀value对)代码详解

2025-05-19 23:57:53
推荐回答(1个)
回答1:

最复杂的就是这一行了:
(word for word in jieba.cut(line,HMM=True)if word not in stop and len(word.strip())>1)
jieba.cut(line)将一行字符串,分割成一个个单词
word for word in jieba.cut(line,HMM=True)是一个Python的表理解,相当于for循环遍历分割好的一个个单词
if word not in stop and len(word.strip())>1这仍然是表理解的一部分,如果满足条件,就把单词加入到一个新的列表中,如果不满足就丢弃,
word not in stop单词不在停用词当中
len(word.strip())>1单词去掉首尾的空格、标点符号后的长度大于1