📌  相关文章
📜  Python|检查给定列表中是否有任何元素出现 n 次

📅  最后修改于: 2022-05-13 01:55:15.589000             🧑  作者: Mango

Python|检查给定列表中是否有任何元素出现 n 次

给定一个列表,任务是找出是否有任何元素在给定的整数列表中出现“n”次。它基本上会检查出现 n 次的第一个元素。
例子:

Input: l =  [1, 2, 3, 4, 0, 4, 3, 2, 1, 2], n = 3
Output :  2

Input: l =  [1, 2, 3, 4, 0, 4, 3, 2, 1, 2, 1, 1], n = 4
Output :  1


以下是一些在Python中完成任务的方法——
方法一:使用简单的迭代和排序

Python3
# Python code to find occurrence of any element
# appearing 'n' times
 
# Input Initialisation
input = [1, 2, 3, 0, 4, 3, 4, 0, 0]
 
# Sort Input
input.sort()
 
# Constants Declaration
n = 3
prev = -1
count = 0
flag = 0
 
# Iterating
for item in input:
    if item == prev:
        count = count + 1
    else:
        count = 1
    prev = item
     
    if count == n:
        flag = 1
        print("There are {} occurrences of {} in {}".format(n, item, input))
        break
 
# If no element is not found.
if flag == 0:
    print("No occurrences found")


Python3
# Python code to find occurrence of any element
# appearing 'n' times
 
# Input list initialisation
input = [1, 2, 3, 4, 0, 4, 3, 4]
 
# Constant declaration
n = 3
 
# print
print("There are {} occurrences of {} in {}".format(input.count(n), n, input))


Python3
# Python code to find occurrence of any element
# appearing 'n' times
 
# importing
from collections import defaultdict
 
# Dictionary declaration
dic = defaultdict(int)
 
# Input list initialisation
Input = [9, 8, 7, 6, 5, 9, 2]
 
# Dictionary populate
for i in Input:
    dic[i]+= 1
 
# constant declaration
n = 2
flag = 0
 
# Finding from dictionary
for element in Input:
    if element in dic.keys() and dic[element] == n:
        print("Yes, {} has {} occurrence in {}.".format(element, n, Input))
        flag = 1
        break
 
# if element not found.
if flag == 0:
    print("No occurrences found")


输出 :

There are 3 occurrences of 0 in [0, 0, 0, 1, 2, 3, 3, 4, 4]


方法 2:使用计数

Python3

# Python code to find occurrence of any element
# appearing 'n' times
 
# Input list initialisation
input = [1, 2, 3, 4, 0, 4, 3, 4]
 
# Constant declaration
n = 3
 
# print
print("There are {} occurrences of {} in {}".format(input.count(n), n, input))

输出 :

There are 2 occurrences of 3 in [1, 2, 3, 4, 0, 4, 3, 4]


方法3:使用defaultdict
我们首先在字典中填充列表项,然后查找任何元素的计数是否等于 n。

Python3

# Python code to find occurrence of any element
# appearing 'n' times
 
# importing
from collections import defaultdict
 
# Dictionary declaration
dic = defaultdict(int)
 
# Input list initialisation
Input = [9, 8, 7, 6, 5, 9, 2]
 
# Dictionary populate
for i in Input:
    dic[i]+= 1
 
# constant declaration
n = 2
flag = 0
 
# Finding from dictionary
for element in Input:
    if element in dic.keys() and dic[element] == n:
        print("Yes, {} has {} occurrence in {}.".format(element, n, Input))
        flag = 1
        break
 
# if element not found.
if flag == 0:
    print("No occurrences found")

输出 :

Yes, 9 has 2 occurrence in [9, 8, 7, 6, 5, 9, 2]