python 列表多个元素如何配对去重呢?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 列表多个元素如何配对去重呢?相关的知识,希望对你有一定的参考价值。
例如list["a","1","b","1","c","2","d","3","e","4","c","3"]
它们的配对关系是a1,b1,c2,d3,e4,c3,字母是商品名称,数字是商品ID,我要根据ID进行去重,只要ID重复,商品名称直接配对删除。最终得到list["a","1","c","2","d","3","e","4"],请问这个代码该怎么写呢?
source = ["a","1","b","1","c","2","d","3","e","4","c","3"]
out = []
for e in zip(source[0::2],source[1::2]):
....if e[1] not in out:
........out = out + list(e)
print(out)本回答被提问者采纳 参考技术B 1.使用set的特型,python的set和其他语言类似, 是一个无序不重复元素集 1 2 3 4 orgList=[1,0,3,7,7,5] #list()方...
2.使用keys()方法 1 2 3 4 orgList=[1,0,3,7,7,5] ...
3.循环遍历法 1 2 3 4 5 6 orgList=[1,0,3,7,7,5] ...
4.按照索引再次排序 1 2 3 4追问
你这个办法不行。用set()方法只是删除了里面1234和abcde的重复项,并没有实现关联删除。数据量不是很多,也就几万条,不想用数据库,考虑过用字典,但是最终还是要重新转换成列表格式,觉得麻烦。
以上是关于python 列表多个元素如何配对去重呢?的主要内容,如果未能解决你的问题,请参考以下文章