如何計算列表中的唯一值 - How do I count unique values inside a list

問題:

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!如果有人可以發佈解決方案,以便我可以從中學習,或者至少告訴我它會很棒,謝謝!


解決方案:

參考一: https://en.stackoom.com/question/pXAW
參考二: https://stackoom.com/question/pXAW
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章