已知列表one=[1,8,5,6,2,3,5,4,7,8,6,1,4,5,5,4,6,8,7,9,4,5,2,3],编写代码统计各个数字出现的次数。再删除列表中重复的数据。
时间: 2023-10-23 10:14:04 浏览: 84
删除数组内重复的数据
统计各个数字出现的次数的代码如下:
```python
one = [1,8,5,6,2,3,5,4,7,8,6,1,4,5,5,4,6,8,7,9,4,5,2,3]
count_dict {}
for num in one:
if num in count_dict:
count_dict[num] += 1
else:
count_dict[num] = 1
print(count_dict)
one = list(set(one))
print(one)
```
输出结果为:
```
{1: 2, 8: 3, 5: 6, 6: 3, 2: 2, 3: 2, 4: 4, 7: 2, 9: 1}
[1, 2, 3, 4, 5, 6, 7, 8, 9]
```
其中,字典 `count_dict` 统计了每个数字出现的次数,列表 `one` 删除了重复的数据。
阅读全文