Python 官方文档:入门教程 => 点击学习
可以通过使用Counter函数来统计数字出现的次数,然后遍历统计结果,找出出现次数为1的数字。以下是一个示例代码:```python
可以通过使用Counter函数来统计数字出现的次数,然后遍历统计结果,找出出现次数为1的数字。以下是一个示例代码:
```python
from collections import Counter
def find_single_number(nums):
count = Counter(nums)
single_numbers = [num for num, freq in count.items() if freq == 1]
return single_numbers
# 测试代码
nums = [1, 2, 3, 4, 5, 4, 3, 2, 1]
single_numbers = find_single_number(nums)
print(single_numbers)
```
输出结果为:[5],表示只出现一次的数字为5。
--结束END--
本文标题: python怎么找出只出现一次的数字
本文链接: https://www.lsjlt.com/news/421246.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0