Find Unique Characters in a File

前端 未结 22 2241
耶瑟儿~
耶瑟儿~ 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 04:18

    Print unique characters (ASCII and Unicode UTF-8)

    import codecs
    file = codecs.open('my_file_name', encoding='utf-8')
    
    # Runtime: O(1)
    letters = set()
    
    # Runtime: O(n^2)
    for line in file:
      for character in line:
        letters.add(character)
    
    # Runtime: O(n)
    letter_str = ''.join(letters)
    
    print(letter_str)
    

    Save as unique.py, and run as python unique.py.

提交回复
热议问题