【python-过滤列表中重复的数据】面试题 过滤给出的列表中重复的数据
2022/7/23 14:25:17
本文主要是介绍【python-过滤列表中重复的数据】面试题 过滤给出的列表中重复的数据,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
两种方法:
1,使用python自带列表去重函数set()
class Test_repeat(): def setup(self): self.data=[1,2,3,4,1,5,6,3,1,7,7,7,7] self.data_expect=[1, 2, 3, 4, 5, 6, 7] def data_repeat(self,data:list): data=set(data) return list(data) def test_repeat(self): assert self.data_expect==self.data_repeat(self.data) 2,使用字典key的唯一性进行去重
class Test_repeat(): def setup(self): self.data=[1,2,3,4,1,5,6,3,1,7,7,7,7] self.data_expect=[1, 2, 3, 4, 5, 6, 7] def data_repeat(self,data): counter={} for i in data: if i not in counter.keys(): counter[i]=1 else: counter[i]=+1 return list(counter.keys()) def test_repeat(self): assert self.data_expect==self.data_repeat(self.data) 注:使用pytest习惯了 写法请轻喷
这篇关于【python-过滤列表中重复的数据】面试题 过滤给出的列表中重复的数据的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python
- 2024-03-12python colon equals