Find Unique Characters in a File

前端 未结 22 2625
耶瑟儿~
耶瑟儿~ 2021-02-04 03:30

I have a file with 450,000+ rows of entries. Each entry is about 7 characters in length. What I want to know is the unique characters of this file.

For instance, if my f

22条回答
  •  半阙折子戏
    2021-02-04 03:59

    Python w/sets (quick and dirty)

    s = open("data.txt", "r").read()
    print "Unique Characters: {%s}" % ''.join(set(s))
    

    Python w/sets (with nicer output)

    import re
    
    text = open("data.txt", "r").read().lower()
    unique = re.sub('\W, '', ''.join(set(text))) # Ignore non-alphanumeric
    
    print "Unique Characters: {%s}" % unique
    

提交回复
热议问题