2

我是 Python 新手。我有一个列表,有重复的条目。他们有什么方法可以计算 UNIQUE 字符串的数量吗?问候,

input1 = input("Name ")
input2 = []
input2.append(input1)
while input1:
  input1 = input("Name ")
  input2.append(input1)

我只想打印输入的唯一名称的数量。谢谢!

4

2 回答 2

3

这个怎么样:

len(set(input2))

len(set(n[5:] for n in input2))
于 2013-09-06T07:18:08.157 回答
2

我们可以使用集合中的计数器

>>> a=['Jack','Jill','Jack']
>>> from collections import Counter
>>> myDict=Counter(a);
>>> myDict


Counter({'Jack': 2, 'Jill': 1})

然后我们可以只使用 myDict 作为字典

于 2013-09-06T08:20:04.593 回答