python - How do I count unique values inside a list


Translate

So I'm trying to make this program that will ask the user for input and store the values in an array / list.
Then when a blank line is entered it will tell the user how many of those values are unique.
I'm building this for real life reasons and not as a problem set.

enter: happy
enter: rofl
enter: happy
enter: mpg8
enter: Cpp
enter: Cpp
enter:
There are 4 unique words!

My code is as follows:

# ask for input
ipta = raw_input("Word: ")

# create list 
uniquewords = [] 
counter = 0
uniquewords.append(ipta)

a = 0   # loop thingy
# while loop to ask for input and append in list
while ipta: 
  ipta = raw_input("Word: ")
  new_words.append(input1)
  counter = counter + 1

for p in uniquewords:

..and that's about all I've gotten so far.
I'm not sure how to count the unique number of words in a list?
If someone can post the solution so I can learn from it, or at least show me how it would be great, thanks!



All Answers
  • Translate

    In addition, use collections.Counter to refactor your code:

    from collections import Counter
    
    words = ['a', 'b', 'c', 'a']
    
    Counter(words).keys() # equals to list(set(words))
    Counter(words).values() # counts the elements' frequency
    

    Output:

    ['a', 'c', 'b']
    [2, 1, 1]
    

  • Translate

    You can use a set to remove duplicates, and then the len function to count the elements in the set:

    len(set(new_words))
    

  • Translate

    Use a set:

    words = ['a', 'b', 'c', 'a']
    unique_words = set(words)             # == set(['a', 'b', 'c'])
    unique_word_count = len(unique_words) # == 3
    

    Armed with this, your solution could be as simple as:

    words = []
    ipta = raw_input("Word: ")
    
    while ipta:
      words.append(ipta)
      ipta = raw_input("Word: ")
    
    unique_word_count = len(set(words))
    
    print "There are %d unique words!" % unique_word_count
    

  • Translate

    values, counts = np.unique(words, return_counts=True)


  • Translate

    For ndarray there is a numpy method called unique:

    np.unique(array_name)
    

    Examples:

    >>> np.unique([1, 1, 2, 2, 3, 3])
    array([1, 2, 3])
    >>> a = np.array([[1, 1], [2, 3]])
    >>> np.unique(a)
    array([1, 2, 3])
    

    For a Series there is a function call value_counts():

    Series_name.value_counts()
    

  • Translate
    ipta = raw_input("Word: ") ## asks for input
    words = [] ## creates list
    unique_words = set(words)
    

  • Translate

    Although a set is the easiest way, you could also use a dict and use some_dict.has(key) to populate a dictionary with only unique keys and values.

    Assuming you have already populated words[] with input from the user, create a dict mapping the unique words in the list to a number:

    word_map = {}
    i = 1
    for j in range(len(words)):
        if not word_map.has_key(words[j]):
            word_map[words[j]] = i
            i += 1                                                             
    num_unique_words = len(new_map) # or num_unique_words = i, however you prefer
    

  • Translate

    The following should work. The lambda function filter out the duplicated words.

    inputs=[]
    input = raw_input("Word: ").strip()
    while input:
        inputs.append(input)
        input = raw_input("Word: ").strip()
    uniques=reduce(lambda x,y: ((y in x) and x) or x+[y], inputs, [])
    print 'There are', len(uniques), 'unique words'
    

  • Translate

    I'd use a set myself, but here's yet another way:

    uniquewords = []
    while True:
        ipta = raw_input("Word: ")
        if ipta == "":
            break
        if not ipta in uniquewords:
            uniquewords.append(ipta)
    print "There are", len(uniquewords), "unique words!"
    

  • Translate
    ipta = raw_input("Word: ") ## asks for input
    words = [] ## creates list
    
    while ipta: ## while loop to ask for input and append in list
      words.append(ipta)
      ipta = raw_input("Word: ")
      words.append(ipta)
    #Create a set, sets do not have repeats
    unique_words = set(words)
    
    print "There are " +  str(len(unique_words)) + " unique words!"
    

  • Translate

    Other method by using pandas

    import pandas as pd
    
    LIST = ["a","a","c","a","a","v","d"]
    counts,values = pd.Series(LIST).value_counts().values, pd.Series(LIST).value_counts().index
    df_results = pd.DataFrame(list(zip(values,counts)),columns=["value","count"])
    

    You can then export results in any format you want


  • Translate
    aa="XXYYYSBAA"
    bb=dict(zip(list(aa),[list(aa).count(i) for i in list(aa)]))
    print(bb)
    # output:
    # {'X': 2, 'Y': 3, 'S': 1, 'B': 1, 'A': 2}