How to return unique words from the text file using Python

后端 未结 9 2188
遇见更好的自我
遇见更好的自我 2021-01-04 23:45

How do I return all the unique words from a text file using Python? For example:

I am not a robot

I am a human

Should return:

9条回答
  •  渐次进展
    2021-01-05 00:16

    Using Regex and Set:

    import re
    words = re.findall('\w+', text.lower())
    uniq_words = set(words)
    

    Other way is creating a Dict and inserting the words like keys:

    for i in range(len(doc)):
            frase = doc[i].split(" ")
            for palavra in frase:
                if palavra not in dict_word:
                    dict_word[palavra] = 1
    print dict_word.keys()
    

提交回复
热议问题