因此,我试图使这个程序,将要求用户输入,并将值存储在一个数组/列表。 然后,当输入空行时,它会告诉用户这些值中有多少是唯一的。 我做这个是出于现实生活的原因,而不是作为习题集。

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

我的代码如下:

# 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:

..到目前为止我就知道这么多 我不知道如何计算一个列表中唯一的单词数? 如果有人可以发布解决方案,这样我就可以从中学习,或者至少向我展示它是如何伟大的,谢谢!


当前回答

你可以使用一个set来删除重复的元素,然后使用len函数来计数set中的元素:

len(set(new_words))

其他回答

你可以使用一个set来删除重复的元素,然后使用len函数来计数set中的元素:

len(set(new_words))
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!"

如何:

import pandas as pd
#List with all words
words=[]

#Code for adding words
words.append('test')


#When Input equals blank:
pd.Series(words).nunique()

它返回列表中有多少个唯一值

使用集合:

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

有了这个,你的解决方案可以很简单:

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

我自己也会用一套,但这里还有另一种方法:

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!"