2.安装Spark与Python练习
2022/3/8 17:14:54
本文主要是介绍2.安装Spark与Python练习,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
with open("earth_song.txt", "r") as f:
text = f.read()
text = text.lower()
for ch in '!@#$%^&*(_)-+=\\[]}{|;:\'\"`~,<.>?/':
text = text.replace(ch, " ")
words = text.split() # 以空格分割文本
stop_words = []
with open('stop_words.txt', 'r') as f: # 读取停用词文件
for line in f:
stop_words.append(line.strip('\n'))
afterwords = []
for i in range(len(words)):
z = 1
for j in range(len(stop_words)):
if words[i] == stop_words[j]:
continue
else:
if z == len(stop_words):
afterwords.append(words[i])
break
z = z + 1
continue
counts = {}
for word in afterwords:
counts[word] = counts.get(word, 0) + 1
items = list(counts.items())
items.sort(key=lambda x: x[1], reverse=True)
f1 = open('count.txt', 'w')
for i in range(len(items)):
word, count = items[i]
f1.write(word+" "+str(count)+"\n")
这篇关于2.安装Spark与Python练习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python