尝试获取重复元素的数量

Trying to get a count of duplicate elements

我是 python 的新手,我只是想计算重复元素的数量。我的代码看起来像这样:

n=int(input("Enter the number of products to be stored in a list : "))

list1=[]

for i in range(n):
  items=int(input("value for product " + str(i+1) + " : "))
    list1.append(items)
    
dup=[]

for a in list1:
    if list1.count(a)>1:
        dup.append(a)

print("Count of duplicate elements in the list: ",dup)

输出:

Enter the number of products in a list : 5
value for product 1 : 4
value for product 2 : 4
value for product 3 : 4
value for product 4 : 1
value for product 5 : 5
Count of duplicate elements in the list:  [4, 4, 4]

答案应该是 1,因为只有数字 4 是重复值,但我得到了上面的输出。有人可以请教吗?

您正在将副本添加到 dup,即使它已经在列表中。您应该在添加之前检查它。

for a in list1:
    if list1.count(a)>1 and a not in dup:
        dup.append(a)

那么如果你想要重复的计数,你应该打印 dup 的长度,而不是它的内容。

print("Count of duplicate elements in the list: ",len(dup))

您也可以将 dup 设为集合而不是列表。然后忽略重复项。通过初始化执行此操作:

dup = set()

并使用 .add() 而不是 .append()

如果 Count 大于 1,检查 a 是否不在 dup 列表中,然后将 a 添加到其中。 最后,打印 dup 列表的长度

n=int(input("Enter the number of products to be stored in a list : "))

list1=[]

for i in range(n):
    item = int(input("value for product " + str(i+1) + " : "))
    list1.append(item)
    
dup = []

for a in list1:
    if (list1.count(a) > 1) and (a not in dup):
        dup.append(a)

print("Count of duplicate elements in the list: ", len(dup))

物有所值,这里有一个更高级的解决方案:

一般统计多个元素出现的次数,使用Counter最简单。一旦你有了它,你只需要获得多次出现的项目数。

list1 = [4, 4, 4, 1, 5]
# ---
from collections import Counter

counts = Counter(list1)  # > Counter({4: 3, 1: 1, 5: 1})
total = sum(count>1 for count in counts.values())

print(total)  # -> 1

sum() 有效,因为 count>1 returns FalseTrue,以及 False == 0True == 1

使用 numpy 库,您可以使用 numpy.unique 来计算您的 numpy 数组中的每个出现次数:

import numpy

for a in list1: 
    if list1.count(a)>1:
        dup.append(a)

dup_array = numpy.array(dup)
unique, counts = numpy.unique(dup, return_counts=True)
dict(zip(unique, counts))

一个选项可能是使用字典,这样您不仅可以计算重复项的数量,还可以知道重复的内容以及重复的次数。

n = int(input("Enter the number of products to be stored in a list : "))  # 

I have entered 5

list1 = []

for i in range(n):
    items = int(input("value for product " + str(i + 1) + " : "))
    list1.append(items)

dup = {}

for a in list1:
    if list1.count(a) > 1:
        dup[a] = dup.get(a, 0)+1

print("Count of duplicate elements in the list: ", len(dup))
print(dup)

结果是:

Enter the number of products to be stored in a list : 5
value for product 1 : 4
value for product 2 : 4
value for product 3 : 4
value for product 4 : 2
value for product 5 : 2
Count of duplicate elements in the list:  2
{4: 3, 2: 2}

很简单。您可以使用 Counter from collections 来计算列表中每个元素的出现次数。然后你可以如下计算不唯一的元素。

from collections import Counter
n=int(input("Enter the number of products to be stored in a list : "))

list1=[]

for i in range(n):
    items=int(input("value for product " + str(i+1) + " : "))
    list1.append(items)
dup_dict=Counter(list1)
count=0
for i in dup_dict.values():
    if(i!=1):
        count+=1

print("Count of duplicate elements in the list: ",count)