在一行文本文件中查找许多唯一的单词

Bha*_*ath 1 python text

我想创建一个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)

如果代码有效,那么在挖掘数据时,它对于更大的文本文件是否会以相同的方式工作?

The*_*nse 5

您可以使用 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)


Tig*_*kT3 5

with open('details.txt', 'r') as f:
    for line in f:
        print(len(set(line.split())))
Run Code Online (Sandbox Code Playgroud)

  • @Vignesh - 是的; 这里的独特算法空间不大. (2认同)