我如何计算在Python数组里面唯一值? [英] How do I count unique values inside an array in Python?

查看:1175
本文介绍了我如何计算在Python数组里面唯一值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想使这个程序会要求用户输入的和值存储在一个数组/列表。然后输入一个空行的时候,它会告诉用户有多少价值是独一无二的。我建立这个现实生活的理由,而不是作为一个问题集。

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!

我的code是如下:

My code is as follows:

ipta = raw_input("Word: ") ## asks for input
uniquewords = [] ## creates list
counter = 0
uniquewords.append(ipta)
a = 0 ## loop thingy
while ipta: ## while loop to ask for input and append in list
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 atleast show me how it would be great, thanks!

推荐答案

您可以使用设置删除重复,然后 LEN 功能算在该组中的元素:

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

len(set(new_words))

这篇关于我如何计算在Python数组里面唯一值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆