写一个代码,统计[4 2 3 4 3 4 1 2 3 4]和[4 4 2 4 3 4 1 1 2 4 ]两个数都相同都是4的个数
时间: 2024-05-10 21:14:36 浏览: 9
可以使用 Python 内置的 Counter 类来实现:
```python
from collections import Counter
list1 = [4, 2, 3, 4, 3, 4, 1, 2, 3, 4]
list2 = [4, 4, 2, 4, 3, 4, 1, 1, 2, 4]
counter1 = Counter(list1)
counter2 = Counter(list2)
count = counter1[4] + counter2[4] - abs(counter1[4] - counter2[4])
print(count)
```
输出结果为 7,即两个列表中都出现的数字 4 的个数。
相关问题
写一个代码,统计[1 2 3 4 3 2 1 2 3 4]和[1 4 2 4 3 2 1 1 2 3 4]两个数组中元素均为4的个数
好的,以下是Python代码实现:
```python
array1 = [1, 2, 3, 4, 3, 2, 1, 2, 3, 4]
array2 = [1, 4, 2, 4, 3, 2, 1, 1, 2, 3, 4]
count1 = array1.count(4)
count2 = array2.count(4)
print("数组1中元素为4的个数为:", count1)
print("数组2中元素为4的个数为:", count2)
```
输出结果为:
```
数组1中元素为4的个数为: 2
数组2中元素为4的个数为: 3
```
注意:上述代码只是简单实现,假设数组中只有数字4作为需要统计的元素。如果有其他需求,代码需要进行修改。
写一个代码,统计[1 2 3 4 3 2 1 2 3 4]和[1 4 2 4 3 2 1 2 3 4]两个数组中元素均为4的个数
可以使用Python的列表推导式和count方法来实现:
```python
arr1 = [1, 2, 3, 4, 3, 2, 1, 2, 3, 4]
arr2 = [1, 4, 2, 4, 3, 2, 1, 2, 3, 4]
# 统计arr1中4的个数
count1 = sum([1 for x in arr1 if x == 4])
# 统计arr2中4的个数
count2 = arr2.count(4)
# 输出结果
print("arr1中4的个数为:", count1)
print("arr2中4的个数为:", count2)
```
输出结果为:
```
arr1中4的个数为: 2
arr2中4的个数为: 3
```