如何计数列表中唯一值的出现

所以我试着让这个程序要求用户输入并将值存储在数组/列表中 然后,当输入一个空行时,它将告诉用户这些值中有多少是唯一的 我构建这个是出于现实生活的原因,而不是作为一个习题集
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:
< p > . .到目前为止,这就是我所得到的一切 我不知道如何计算一个列表中唯一的单词数?< br > 如果有人可以发布解决方案,这样我就可以从中学习,或者至少向我展示它是如何伟大的,谢谢!< / p >
498167 次浏览

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

len(set(new_words))

使用:

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
ipta = raw_input("Word: ") ## asks for input
words = [] ## creates list
unique_words = set(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'

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

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!"
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!"
另外,使用集合。计数器重构你的代码:

from collections import Counter


words = ['a', 'b', 'c', 'a']


Counter(words).keys() # equals to list(set(words))
Counter(words).values() # counts the elements' frequency

输出:

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

虽然使用set是最简单的方法,但你也可以使用dict并使用some_dict.has(key)来填充一个只有唯一键和值的字典。

假设你已经用用户的输入填充了words[],创建一个字典,将列表中唯一的单词映射到一个数字:

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

对于ndarray,有一个名为独特的的numpy方法:

np.unique(array_name)

例子:

>>> 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])

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

Series_name.value_counts()

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

更详细地

import numpy as np


words = ['b', 'a', 'a', 'c', 'c', 'c']
values, counts = np.unique(words, return_counts=True)

函数numpy.unique返回输入列表中的排序个唯一元素及其计数:

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

另一种方法是用熊猫

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"])

然后,您可以将结果导出为所需的任何格式

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}

如何:

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


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




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

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

如果你想要一个唯一值的直方图,这里是lineer

import numpy as np
unique_labels, unique_counts = np.unique(labels_list, return_counts=True)
labels_histogram = dict(zip(unique_labels, unique_counts))

你可以使用get方法:

lst = ['a', 'b', 'c', 'c', 'c', 'd', 'd']


dictionary = {}
for item in lst:
dictionary[item] = dictionary.get(item, 0) + 1
    

print(dictionary)

输出:

{'a': 1, 'b': 1, 'c': 3, 'd': 2}

这是我自己的版本

def unique_elements():
elem_list = []
dict_unique_word = {}
for i in range(5):# say you want to check for unique words from five given words
word_input = input('enter element: ')
elem_list.append(word_input)
if word_input not in dict_unique_word:
dict_unique_word[word_input] = 1
else:
dict_unique_word[word_input] += 1
return elem_list, dict_unique_word
result_1, result_2 = unique_elements()
# result_1 holds the list of all inputted elements
# result_2 contains unique words with their count
print(result_2)