Find Unique Characters in a File

前端 未结 22 2281
耶瑟儿~
耶瑟儿~ 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:07

    Python using a dictionary. I don't know why people are so tied to sets or lists to hold stuff. Granted a set is probably more efficient than a dictionary. However both are supposed to take constant time to access items. And both run circles around a list where for each character you search the list to see if the character is already in the list or not. Also Lists and Dictionaries are built in Python datatatypes that everyone should be using all the time. So even if set doesn't come to mind, dictionary should.

    file = open('location.txt', 'r')
    letters = {}
    for line in file:
      if line == "":
        break
      for character in line.strip():
        if character not in letters:
          letters[character] = True
    file.close()
    print "Unique Characters: {" + "".join(letters.keys()) + "}"
    

提交回复
热议问题