如何在Python中计算数组中的唯一值?

时间:2022-05-28 21:23:33

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!

. .这就是我到目前为止所得到的。我不知道如何计算一个列表中唯一的单词数?如果有人能发布解决方案,我就可以从中学习,或者至少告诉我该如何做,谢谢!

9 个解决方案

#1


110  

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

您可以使用集合来删除重复,然后使用len函数来计算集合中的元素:

len(set(new_words))

#2


96  

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

#3


13  

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

#4


1  

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.

尽管设置是最简单的方法,但是您也可以使用一个dict语句并使用some_dict.has(key)来仅用惟一的键和值填充字典。

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

#5


1  

For ndarray there is a numpy method called unique:

对于ndarray,有一个叫unique的numpy方法:

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():

对于一个系列,有一个函数调用value_counts():

Series_name.value_counts()

#6


0  

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

#7


0  

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

下面的工作。lambda函数过滤掉重复的单词。

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'

#8


0  

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

#9


0  

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

#1


110  

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

您可以使用集合来删除重复,然后使用len函数来计算集合中的元素:

len(set(new_words))

#2


96  

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

#3


13  

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

#4


1  

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.

尽管设置是最简单的方法,但是您也可以使用一个dict语句并使用some_dict.has(key)来仅用惟一的键和值填充字典。

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

#5


1  

For ndarray there is a numpy method called unique:

对于ndarray,有一个叫unique的numpy方法:

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():

对于一个系列,有一个函数调用value_counts():

Series_name.value_counts()

#6


0  

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

#7


0  

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

下面的工作。lambda函数过滤掉重复的单词。

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'

#8


0  

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

#9


0  

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