📌  相关文章
📜  Python|查找列表中给定元素的频率总和

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

Python|查找列表中给定元素的频率总和

给定两个包含整数的列表,任务是找到第二个列表中第一个列表元素的频率总和。

例子:

Input: list1 = [1, 2, 3]
       list2 = [2, 1, 2, 1, 3, 5, 2, 3]

Output: 7

Explanation:
No of time 1 occurring in list2 is :2
No of time 2 occurring in list2 is :3
No of time 3 occurring in list2 is :2
Sum = 2+3+2 = 7

以下是实现上述任务的一些方法。

方法 #1:使用sum()

# Python code to find sum of frequency of 
# element of first list in second list.
  
# List initialization
Input1 = [1, 2, 3]
Input2 = [2, 1, 2, 1, 3, 5, 2, 3]
  
# Using sum
Output = sum(Input2.count(elem) for elem in Input1)
  
# Printing output
print("Initial list are:", Input1, Input2)
print("Frequency is:", Output)
输出:
Initial list are: [1, 2, 3] [2, 1, 2, 1, 3, 5, 2, 3]
Frequency is: 7


方法#2:使用sum()Counter()

from collections import Counter
  
# List initialization
Input1 = [1, 2, 3]
Input2 = [2, 1, 2, 1, 3, 5, 2, 3]
  
  
temp = Counter(Input2)
Output = sum(temp[x] for x in Input1)
  
# Printing output
print("Initial list are:", Input1, Input2)
print("Frequency is:", Output)
输出:
Initial list are: [1, 2, 3] [2, 1, 2, 1, 3, 5, 2, 3]
Frequency is: 7