对于自己使用requests创写的类及方法想要对数据进行去重入库
问题1:如何做到数据去重处理?
答:使用set,并且set能够自动实现去重效果,可以配合列表使用。
问题2:当列表中有大数据怎么提高读取速度?
答:转换为set类型。
a = [1, 4, 7, 2, 1, 8, 7]
b = set(a)
print(type(b), b)
>>> <class 'set'> {1, 2, 4, 7, 8}
c = [i for i in b]
print(type(c), c)
>>> <class 'list'> [1, 2, 4, 7, 8]
for i in b:
print(type(i), i)
>>> <class 'int'> 1
>>> <class 'int'> 2
>>> <class 'int'> 4
>>> <class 'int'> 7
>>> <class 'int'> 8
重点来了,如何使用?
mylist = ['zhangsan', 'zhangsi', 'zhangwu']
myset = set(mylist)
name = 'zhangsan'
if name not in myset:
myset.add(name)
print(myset)
else:
print('已存在')
>>>已存在
ps:列表读取大数据时能卡到爆炸,set读取熟读最快,dict其次,不信你可以测试下!!!!!