请输入一个列表,快速计算并输出列表中只出现一次的元素
时间: 2024-05-09 19:17:50 浏览: 15
Sure, here's a solution in Python:
```
from collections import Counter
def find_unique(lst):
# Count the occurrences of each element in the list
count = Counter(lst)
# Find the elements that only appear once
unique = [elem for elem, cnt in count.items() if cnt == 1]
# Return the list of unique elements
return unique
my_list = [1, 2, 3, 4, 4, 5, 6, 7, 8, 7, 9]
unique_elements = find_unique(my_list)
print(unique_elements)
```
This code uses the `Counter` class from the `collections` module to count the occurrences of each element in the list. Then it filters out the elements that appear more than once, leaving only the unique elements. Finally, it returns the list of unique elements, which can then be printed or used for further processing.
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)