python中列表去重的一个特殊情况求助
#初始列表org_list=['a','a','a','a','b','b','c','c','d','d','1','1','1','1']
#想要达到的去重效果
res_list=['a','a','b','c','d','1','1']
即不是对org_list完全去重,而且去重一半的重复数据,这个怎么写呀? 3个数据怎么办? 从头开始判断元素呗,取每一个跟前一个判断是否重复,重复则舍弃,同时指针加一,取下一个作为比对元素,下下一个开始比对 org_list=['a','a','a','a','b','b','c','c','d','d','1','1','1','1']
res_list = list()
tmp_set = set()
for i in org_list:
if i in tmp_set:
tmp_set.remove(i)
else:
tmp_set.add(i)
res_list.append(i)
print(res_list)
这样?改吧改吧?
def DeleteHalf(org):
ileft = 0
iright = 1
temp_len = 1
org_len = len(org)
#注意处理最后一个
while iright <= org_len:
if iright < org_len and org == org:
temp_len+=1
else:
for i in range(int(temp_len / 2)):
org = org
ileft+=1
temp_len = 1
iright+=1
return ileft
if __name__ == '__main__':
org_list = ['a', 'a', 'a', 'a', 'b', 'b', 'c', 'c', 'd', 'd', '1', '1', '1', '1']
# 排序
length = DeleteHalf(org_list)
org_list = org_list[:length]
print(org_list) 可以这样
遍历,用list.count(2)统计个数,大于2就del list… org_list=['a','a','a','a','b','b','c','c','d','d','1','1','1','1']
for i in org_list:
if org_list.count(i)>2:
org_list.remove(i)
print(org_list) pzx521521 发表于 2022-4-8 11:31
def DeleteHalf(org):
ileft = 0
iright = 1
{:1_893:}完美实现! pzx521521 发表于 2022-4-8 11:31
def DeleteHalf(org):
ileft = 0
iright = 1
for i in range(int(temp_len / 2)):
org = org
ileft+=1
temp_len = 1
temp_len = 1这句代码起什么作用? org_list=['a','a','a','a','b','b','c','c','d','d','1','1','1','1']
for i in range(len(org_list)-1,-1,-1):
if org_list.count(org_list)>2:
del org_list
print(org_list)
页:
[1]
2