我想创建一个Python程序来查找文本文件中一行中的唯一单词.
文本文件"details"具有以下行
My name is crazyguy
i am studying in a college and i travel by car
my brother brings me food for eating and we will go for shopping after food.
Run Code Online (Sandbox Code Playgroud)
它必须返回输出为
4
10 #(since i is repeated)
13 #(Since food and for are repeated)
Run Code Online (Sandbox Code Playgroud)
如果代码有效,那么在挖掘数据时,它对于更大的文本文件是否会以相同的方式工作?
您可以使用 set traverse through all line split 来创建 lsit 并使其设置为查找唯一值并找到它的计数
with open("filename","r") as inp:
for line in inp:
print len(set(line.split()))
Run Code Online (Sandbox Code Playgroud)
with open('details.txt', 'r') as f:
for line in f:
print(len(set(line.split())))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
921 次 |
| 最近记录: |